JavaScript 《JavaScript Enlightenment》的一个英文 js 知识检查表

chenge · 2013年06月20日 · 2317 次阅读


✴ An object is made up of named properties that store values. ✴ Most everything in JavaScript can act like an object. Complex values are, well, objects and primitive values can be treated like objects This is why you may hear people say that everything in JavaScript is an object. ✴ Objects are created by invoking a constructor function with the new keyword, or by using a shorthand literal expression. ✴ Constructor functions are objects (Function() objects), thus, in JavaScript, objects create objects. ✴ JavaScript offers 9 native constructor functions: Object(), Array(), String(), Number(), Boolean(),Function(),Date(),RegExp(),andError(). TheString(),Number(),and Boolean() constructors are dual-purposed in providing a.) primitive values and b.) object wrappers when needed, so that primitive values can act like objects when so treated. ✴ The values null, undefined, "string", 10, true, and false are all primitive values, without an object nature unless treated like an object. ✴ When the Object(), Array(), String(), Number(), Boolean(), Function(), Date(), RegExp(), and Error() constructor functions are invoked using the new keyword, an object is created that is known as a "complex object" or "reference object". ✴ "string", 10, true, and false, in their primitive forms, have no object qualities until they are used as objects; then JavaScript, behind the scenes, creates temporary wrapper objects so that such values can act like objects. 138 ✴ Primitive values are stored by value, and when copied, are literally copied. Complex object values, on the other hand, are stored by reference, and when copied, are copied by reference. ✴ Primitive values are equal to other primitive values when their values are equal, whereas complex objects are equal only when they reference the same value. That is: a complex value is equal to another complex value when they both refer to the same object. ✴ Due to the nature of complex objects and references, JavaScript objects have dynamic properties. ✴ JavaScript is mutable, which means that native objects and user-defined object properties can be manipulated at any time. ✴ Getting/setting/updating an objectʼs properties is done by using dot notation or bracket notation. Bracket notion is convenient when the name of the object property being manipulated is in the form of an expression (e.g Array['prototype']['join'].apply()). ✴ When referencing object properties, a lookup chain is used to first look at the object that was referenced for the property; if the property is not there, the property is looked for on the constructor functionʼs prototype property. If itʼs not found there, because the prototype holds an object value and the value is created from the Object() constructor, the property is looked for on the Object() constructorʼs prototype property (Object.prototype). If the property is not found there, then the property is determined to be undefined. ✴ The Prototype lookup chain is how inheritance (a.k.a prototypal inheritance) was design to be accomplished in JavaScript. ✴ Because of the object property lookup chain (aka prototypal inheritance), all objects inherit from Object() simply because the prototype property is, itself, an Object() object. ✴ JavaScript functions are first-class citizens: functions are objects with properties and values. ✴ The this keyword, when used inside a function, is a generic way to reference the object containing the function. ✴ The value of this is determined during runtime based on the context in which the function is called. ✴ Used in the global scope, the this keyword refers to the global object. ✴ JavaScript uses functions as a way to create a unique scope. 139 ✴ JavaScript provides the global scope, and itʼs in this scope that all JavaScript code exists. ✴ Functions (specifically, encapsulated functions) create a scope chain for resolving variable lookups. ✴ The scope chain is set up based on the way code is written, not necessarily by the context in which a function is invoked. This permits a function to have access to the scope in which it was originally written, even if the function is called from a different context. This result is known as a closure. ✴ Function expressions and variables declared inside a function without using var become global properties. However, function statements inside of a function scope remain defined in the scope in which they are written. ✴ Functions and variables declared (without var) in the global scope become properties of the global object. ✴ Functions and variables declared (with var) in the global scope become global variables.

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册