首页 > 网站 > WEB开发 > 正文

JavaScript: The Good Parts

2024-04-27 14:16:03
字体:
来源:转载
供稿:网友

javaScript: The Good Parts

Chapter 1 Good Parts:

Javascript is an important language because it is the language of the web browser.

The very good ideas include functions, loose typing, dynamic objects, and an exPRessive object literal notation. The bad ideas include a programming model based on global variables.

The Web has become an important platform for application development, and JavaScript is the only language that is found in all browsers.

Chapter 2 Grammar:

Javascript中的保留字,但是大部分却没在这个语言中使用。undefined,NaN,Infinity也应该算保留字。

abstract boolean break byte case catch char class const continue debugger default delete do doubleelse enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var volatile void while with
Reserved Words

JavaScript has a single number type. Internally, it is represented as 64-bit floating point, the same as Java's double.1 and 1.0 are the same value.

NaN is not equal to any value, including itself. You can detect NaN with the isNaN(number) function. JavaScript has a Math object that contains a set of methods that act on numbers. Math.floor(number) method can be used to convert a number into an integer.

All characters in JavaScript are 16 bits wide. "A" === "/u0041". Strings are immutable.

A block is a set of statements wrapped in curly braces. Unlike many other languages, blocks in JavaScript do not create a new scope, so variables should be defined at the top of the function, not in blocks.

Below are the falsy values:

falsenullundefinedThe empty string ''The number 0The number NaN
Falsy values

for inloop enumerates the property names (or keys) of an object. When iterating an array, use for (i = 0; i < arr.length; i++) { }. It is usually necessary to test object.hasOwnProperty(variable) to determine whether the property name is truly a member of the object or was found instead on the prototype chain.

var arr = [1,2,3,4,5,6,7,8,9];arr[12] = 34;for (var i in arr) {    console.log(i + " - " + arr[i]);  }for (var i = 0; i < arr.length; i++) {    console.log(arr[i]);}
For loop

The try statement executes a block and catches any exceptions that were thrown by the block. The catch clause defines a new variable that will receive the exception object.

The throw statement raises an exception. If the throw statement is in a try block, then control goes to the catch clause. Otherwise, the function invocation is abandoned, and control goes to the catch clause of the try in the calling function.

JavaScript does not allow a line end between the return and the expression.

Chapter 3 Objects:

The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined. All other values are objects. In JavaScript, arrays, functions, regular expressions, and objects are 'objects'.

The || Operator can be used to fill in default values:

var middle = stooge["middle-name"] || "none";var status = flight.status || "unknown";
|| operator

Attempting to retrieve values from undefined will throw a TypeError exception. This can be guarded against with the && operator:

flight.equipment                                         // undefinedflight.equipment.model                               // throw "TypeError"flight.equipment && flight.equipment.model // undefined
&& operator

Objects are passed around by reference. They are never copied.

All objects created from object literals are linked to Object.prototype, an object that comes standard with JavaScript.

if (typeof Object.create !== 'function') {    Object.create = function (o) {        var F = function () { };        F.prototype = o;        return new F();    };}var another_stooge = Object.create(stooge);
Prototype

If we add a new property to a prototype, that property will immediately be visible in all of the objects that are based on that prototype.

The hasOwnProperty method does not look at the prototype chain.

Global abatement => Use a global object to create a top-level structure or use closure for information hiding.

Chapter 4 Functions:

- The method invocation pattern

var myObject = {    value: 0,    increment: function  (inc) {        this.value += typeof inc === 'number' ? inc : 1;    }};myObject.increment(1);document.writeln(myObject.value);  //1myObject.increment(2);document.writeln(myObject.value);  // 3
Example

- The function invocation pattern

When a function is not the property of an obje

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表