JavaScript prototype mode (enumeration object's properties)

Source: Internet
Author: User
Tags hasownproperty

1. Understanding prototype Objects
functionPerson () {} Person.prototype.name= "Nicholas"; Person.prototype.age= 28; Person.prototype.job= "Software Engineer"; Person.prototype.sayName=function() {alert ( This. Name);        }; varPerson1 =NewPerson ();      Person1.sayname (); //Nicholas        varPerson2 =NewPerson ();      Person2.sayname (); //Nicholasalert (person1.sayname ()= = Person2.sayname ())//true


The argument to the isPrototypeOf () method is an instance object that determines the relationship of the prototype object to the instance object when the prototype object calls the isPrototypeOf () method
Alert (Person.prototype.isPrototypeOf (Person1));     True    alert (Person.prototype.isPrototypeOf (Person2));     True
The Object.getprototypeof () method is used to get the prototype of the object, which is the instance object
    Alert (object.getprototypeof (person1) = = Person.prototype);      True    alert (object.getprototypeof (person1). name);     "Nicholas"

Note! : When a property or method modified in an instance overrides properties and methods in the prototype

The hasOwnProperty () method can detect whether a property exists in the instance
function person () {
}

Person.prototype.name = "Nicholas";
Person.prototype.age = 28;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function () {
alert (this.name);
};

var person1 = new Person ();
var person2 = new Person ();


var dd= "DD"; Alert (Person1.hasownproperty ("name")); False alert (Person1.hasownproperty ("DD")); False

Returns true Person1.name = "new" only if the Name property is overridden on the instance; Alert (Person1.hasownproperty ("name")) //true

2, prototype and in operator: There are two ways to use the In operator, used separately and in the for-in loop. When used alone, the in operator returns True when the given property is accessible through the object, whether the attribute exists in an instance or in a prototype.

A, using in loop

 functionPerson () {} Person.prototype.name= "Nicholas"; Person.prototype.age= 28; Person.prototype.job= "Software Engineer"; Person.prototype.sayName=function() {alert ( This. Name);    }; varPerson1 =NewPerson (); varPerson2 =NewPerson (); Alert (Person1.hasownproperty ("Name"));//falseAlert ("Name"inchPerson1);//truePerson1.name= "Greg";                    alert (person1.name); //"Greg"-from the exampleAlert (Person1.hasownproperty ("name"));//trueAlert ("Name"inchPerson1)//true

Using both the hasOwnProperty () method and the In operator, you can determine whether the attribute is present in the object or in the prototype.

b, using the for-in loop:

When you use the for-in loop, you return all the enumerable properties that can be accessed through the object, including the attributes that exist in the instance, as well as the properties that exist in the prototype. Instance properties that mask non-enumerable properties in the prototype are also returned in the for-in loop, so that all developer-defined attributes are enumerable, according to the rules.

var o = {
Tostring:function () {
Return "My Object";
}
};
 for (var in o) {       if (prop = = "ToString") {           alert ("Found tostring") );       }   }   Alert (prop)

C, get all enumerable instance properties on the object: Object.keys ()

functionPerson () {} Person.prototype.name= "Nicholas"; Person.prototype.age= 28; Person.prototype.job= "Software Engineer"; Person.prototype.sayName=function() {alert ( This. Name);    }; varKeys =Object.keys (Person.prototype);        alert (keys); //"Name,age,job,sayname"    varP1 =NewPerson (); P1.name= "Rob"; P1.age= 31; varP1keys =Object.keys (p1);      alert (P1keys); //"Name,age"

Get all the attributes you want, whether or not he can enumerate: Object.getownpropertynames ()

 

functionPerson () {} Person.prototype.name= "Nicholas"; Person.prototype.age= 28; Person.prototype.job= "Software Engineer"; Person.prototype.sayName=function() {alert ( This. Name);    }; varperson =NewPerson (); Person.name= "DFAs"; varKeys =object.getownpropertynames (Person.prototype);        alert (keys); //"Constructor,name,age,job,saynameAlert (object.getownpropertynames (person))//name

Both the Object.keys () and Object.getownpropertynames () methods can be used instead of the for-in loop

JavaScript prototype mode (enumeration object's properties)

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.