aka:JavaScript:Objects
- What version of spec are you referring to write this?
- What is an object?
- An object is a collection of properties.
- What is a property?
- A property associates a name with a set of attributes.
- There are three types of properties: a named data property, a named accessor property, and an internal property.
- What are attributes?
- Attributes are used in JavaScript to define and explain the state of named properties.
- Attributes of a named data property: [[ Value ]], [[ Writable ]], [[ Enumerable ]], [[ Configurable ]]
- Attributes of a named accessor property: [[ Get ]], [[ Set ]], [[ Enumerable ]], [[ Configurable ]]
- What is an internal property?
- http://www.ecma-international.org/ecma-262/5.1/#sec-8.6.2
- "This specification uses various internal properties to define the semantics of object values. These internal properties are not part of the ECMAScript language. They are defined by this specification purely for expository purposes. An implementation of ECMAScript must behave as if it produced and operated upon internal properties in the manner described here. The names of internal properties are enclosed in double square brackets [[ ]]. When an algorithm uses an internal property of an object and the object does not implement the indicated internal property, a TypeError exception is thrown."
- Internal properties common to all objects: [[ Prototype ]], [[ Class ]], [[ Extensible ]], [[ Get ]], [[ GetOwnProperty ]], [[ GetProperty ]], [[ Put ]], [[ CanPut ]], [[ HasProperty ]], [[ Delete ]], [[ DefaultValue ]], [[ DefineOwnProperty ]].
- Internal properties only defined for some objects: [[ PrimitiveValue ]], [[ Construct ]], [[ Call ]], [[ HasInstance ]], [[ Scope ]], [[ FormalParameters ]], [[ Code ]], [[ TargetFunction ]], [[ BoundThis ]], [[ BoundArguments ]], [[ Match ]], [[ ParameterMap ]].
- What is [[ Prototype ]]?
- "All objects have an internal property called [[ Prototype ]]. The value of this property is either null or an object and is used for implementing inheritance. Whether or not a native object can have a host object as its [[ Prototype ]] depends on the implementation. Every [[ Prototype ]] chain must have finite length (that is, starting from any object, recursively accessing the [[ Prototype ]] internal property must eventually lead to a null value). Named data properties of the [[ Prototype ]] object are inherited (are visible as properties of the child object) for the purposes of get access, but not for put access. Named accessor properties are inherited for both get access and put access."
- "Every ECMAScript object has a Boolean-valued [[ Extensible ]] internal property that controls whether or not named properties may be added to the object. If the value of the [[ Extensible ]] internal property is false then additional named properties may not be added to the object. In addition, if [[ Extensible ]] is false the value of the [[ Class ]] and [[ Prototype ]] internal properties of the object may not be modified. Once the value of an [[ Extensible ]] internal property has been set to false it may not be subsequently changed to true."
- "NOTE This specification defines no ECMAScript language operators or built-in functions that permit a program to modify an object’s [[ Class ]] or [[ Prototype ]] internal properties or to change the value of [[ Extensible ]] from false to true. Implementation specific extensions that modify [[ Class ]], [[ Prototype ]] or [[ Extensible ]] must not violate the invariants defined in the preceding paragraph."
- NOTE You can see values of [[ Prototype ]] through the "__proto__" property on Chrome.
- What is native object?
- "object in an ECMAScript implementation whose semantics are fully defined by this specification rather than by the host environment."
- There are two kinds of native objects: bult-in and defined-by-user. Built-in objects are defined in the spec such as Object and Array, while defined-by-user objects are defined by programmers in their scripts such as through constructors.
- What is host object?
- "object supplied by the host environment to complete the execution environment of ECMAScript"
- "NOTE Any object that is not native is a host object."
- What is prototype? I mean, not [[ Prototype ]] nor 'prototype' property, the concept of prototype itself.
- "object that provides shared properties for other objects"
- 'NOTE When a constructor creates an object, that object implicitly references the constructor’s “prototype” property for the purpose of resolving property references. The constructor’s “prototype” property can be referenced by the program expression constructor.prototype, and properties added to an object’s prototype are shared, through inheritance, by all objects sharing the prototype. Alternatively, a new object may be created with an explicitly specified prototype by using the Object.create built-in function.'
- Can I change the value of 'prototype' property of an object after creating it?
- You can do it on not built-in objects but user-created objects.
- What properties does a for-in enumeration show?
- Properties with [[ Enumerable ]] true are shown.
- How can I set [[ Prototype ]] of an object?
- Built-in objects: it's defined in the spec.
- Defined-by-user objects:
- Created from literals: automatically set to Object.prototype.
- Created by calling Object.create(obj): set to obj.
- Created by calling the 'new' operator with a constructor: set to the constructor's.prototype.
- Then, what is the prototype of 'function Obj() { this.a = 0; }'?
- It's an object by new Object(), with the function definition, { this.a = 0; }, as its constructor property.
- But note that the 'constructor' property has no practical use internally. The 'new' operator uses [[ Construct ]] method in a constructor function to initialise an object.
- How does an object use it's [[ Prototype ]]?
- When an object itself doesn't have a property, it follows its [[ Prototype ]] chain to find the property. If it find it, it'll return the value. Otherwise, it'll return 'undefined.'
- How does an object use it's 'prototype'?
- The new operator uses it as the [[ Prototype ]] of the object it creates. This is the sole usage of the property. There is no prototype chain for 'prototype' properties.
- You can use a constructor's.prototype to affect [[ Prototype ]] chains because a constructor's prototype === the [[ Prototype]] of objects created by calling the 'new' operator with the constructor.
- What is 'instanceof'?
- It uses Function object's [[ HasInstance ]] methods. A [[ HasInstance ]] method compares a given value and the function's 'prototype' property. If they refers the same object, it returns true, otherwise, false.