JavaScript objects, functions (JavaScript you don't know)

Source: Internet
Author: User
Tags object object

first, the object
var obj = {};obj[true] = "foo"; obj[3] = "bar"; obj[obj] = "Baz"; obj["true"];obj["3"];obj["[Object Object]";
second, arrays are objects
var ary = ["foo", "bar"];ary.baz = "baz"; ary.length;//3ary.baz;/* if you try to add a property to an array, but the property name "looks" like a number, it becomes a numeric subscript */ary[ "4"] = "baz"; ary.length;//5ary[4];
third, object propertieswritable: Writable (modified)
Enumerable: Enumerable (for ... in)
Configurable: Configurable (config, delete)
Note: Delete can only delete the object (the removable) property, that is, the property that configurable is true.
var obj = {};object.defineproperty (obj, "a", {enumerable:true, value:2}); Object.defineproperty (obj, "B", {enumerable: False, Value:3});/* Checks whether the given property name exists directly in the object (not on the prototype chain) and satisfies the Enumerable:true */obj.propertyisenumerable ("a"); Trueobj.propertyisenumerable ("B");//false/* returns an array that contains all (itself) enumerable properties */object.keys (obj);//["A"]/* returns an array containing all (itself) attributes */ Object.getownpropertynames (obj);//["A", "B"]
Note: In and hasOwnProperty (..) The difference is whether to find [[Prototype]] chains, however, Object.keys (..) and Object.getownpropertynames (..) Only the properties that are directly contained by the object are found.
Four, the object prototypeGet Object prototype: object.getpropertyof (..)
function Foo () {}var foo = new Foo (); Foo.prototype = = = Object.getprototypeof (foo);//true
Note: In a class-oriented language, a class can be copied (instantiated) multiple times, as if it were made with a stencil. In JavaScript, there is no similar replication mechanism. You cannot create multiple instances of a class, you can create more than one object, and they [[property]] are associated with the same object. This makes it possible to access the properties and methods of the object through a delegate.
class Theory:After the construction is complete, you usually only need to manipulate these instances (not the classes), because each instance has all the behaviors you need to complete the task.
v. PROTOTYPE chain [[prototype]]The [[prototype]] mechanism is an internal link that exists in an object that references other objects.
var foo = {something:function () {//...}}; var bar = object.create (foo);//Create a new object bar and associate it to the object foo.
Object.create (NULL) creates an object that has an empty link that cannot be delegated and does not have a prototype chain, so instanceof always returns false. It is ideal for storing data without interference from the prototype chain!
The relationship between objects is not a copy but a delegate!!!
Talking about the prototype chain has to mention the type checking that we often have in JavaScript! That is introspection: Examine the type of the instance, and the main purpose is to determine the structure and function of the object by creating the method.
Way one: a instanceof Foo
Way two: "Duck Type": It must be a duck if it looks like a duck and it sounds like a duck.
if (a.something) {a.something ();//Something () is not necessarily on the A itself}
Way three: isPrototypeOf (), getprototypeof ()
recommended use way three!!!
Vi. Functionsfunctions in JavaScript cannot be copied (in a standard, reliable way), so you can only share references to function objects. This means that if you modify a shared function, such as adding a property, all references will be modified!
Vii. Constructor Functions
function Foo () {}foo.prototype = {};var A1 = new Foo (); a1.constructor = = = Foo;//falsea1.constructor = = Object;//true
Detailed:
Creates a new object and replaces the default. Prototype object reference for the function, then the new object is not automatically obtained. Constructor property.
A1 does not have a. constructor property, so it delegates the Foo.prototype on the [[Prototype]] chain. But this object does not have a. constructor property, so it will continue to delegate to the Object.protoype at the top of the chain.
In fact, the object's. Constructor will point to a function by default, which can be referenced by the object's. prototype.
in short, constructor does not mean to be constructed!!! Changing prototype does not completely change the inheritance relationship!!!
function Foo (name) {this.name = "Fenfei"}function Bar () {}

Error Understanding One:

Bar.prototype = Foo.prototype; Bar.prototype.myTest = 123;new Foo (). mytest;//123
Does not create a new object associated to Bar.prototype, it simply lets Bar.prototype directly reference the Foo.prototype object. The Foo.prototype object itself is modified directly when an assignment statement similar to Bar.prototype.myTest = 123 is executed.

Error Understanding Two:

Bar.prototype = new Foo (); New Bar ("Camile"). Name;//fenfei
It does create a new object that is associated to the Bar.prototype. But it uses the "constructor call" of Foo (), which affects the descendants of bar () if Foo has side effects (such as registering to another object, adding data attributes to this, and so on).

Correct handling mode one [ES6]:

Bar.prototype = Object.create (Foo.prototype); Bar.prototype.myTest = 123;new Foo (). Mytest;//undefinednew Bar ("Camile"). name;//undefined

Correct handling Mode II [after ES6]:

Object.setprototypeof (Bar.prototype, Foo.prototype); Bar.prototype.myTest = 123;new Foo (). Mytest;//undefinednew Bar ("Camile"). name;//undefined
Viii. Functional Relationships(1) Are there any objects pointing to foo.prototype in the whole [[prototype]] chain of a?
a instanceof Foo;
(2) Is there any foo.prototype in the whole [[prototype]] chain of a?
Foo.prototype.isPrototypeOf (a);//do not need to indirectly reference the function Foo, its prototype property is automatically accessed.
(3) Difference
The isPrototypeOf () method can determine the relationship between objects.
Does b appear in the [[prototype]] chain of c?
B.isprototypeof (c);
Example:
function Foo () {}var a = new Foo (); Console.log (a instanceof foo);//Trueconsole.log (Foo.prototype.isPrototypeOf (a));// Truevar B = {};var c = object.create (b); Console.log (B.isprototypeof (c));//Trueconsole.log (b instanceof c);//TypeError

In traditional class-oriented languages, the class definition is not modified after it is made, so the design pattern of the class does not support modification. But one of the most powerful features of JavaScript is its dynamic nature, and any object definition can be modified (unless you set it to immutable)!

[reprint Please indicate source:http://blog.csdn.net/ligang2585116]

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Reprint please indicate source: http://blog.csdn.net/ligang2585116!

JavaScript objects, functions (JavaScript you don't know)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.