The prototype of javascript-learning note-taking record

Source: Internet
Author: User
Tags hasownproperty

1. Prototype is a property of a class, in other words, a static property

2. Ptototype and __proto__, the former is the property of the class, the latter is the object property

3. Examples of prototype and __proto__

<script>
function person () {
This.age = 22;
This.sex = "Man";
}
Person.prototype.showInfo = function () {
Return "It is first time-to-public-what I learning!" + "Person Object-is" + This.age + ", sex is" + this.sex;
}
var p = new person ();
P.age = 23;
P.sex = "also man";
P.__proto__.showmsg = function () {
Return "My name is Askmewhy.i ' m" +this.age + "years old and I ' m" + this.sex;
}
document.write (New Person ()). Showinfo ());
document.write ("<br>");
document.write (P.showmsg ());
</script>

It is also important to note that when you look at it, you always think that the prototype reference is to the parent class, so it is assumed that Person.prototype is adding properties to the parent class object. There are two ways to detect whether an object or class contains a specified property:

<script>//var p = new person (); At first I thought it was true or false.document.write (P.hasownproperty ("Showinfo"));//true (Error) correct answer: false
document.write ("<br>");
Initialize an existing property
Document.wriete (P.hasownproperty ("Age")); True
document.write ("<br>"); //var o = new Object ();document.write (O.hasownproperty ("Showinfo"));//False
document.write ("<br>"); //Classdocument.write (Object.hasownproperty ("Showinfo"));//false
Summary: The method cannot check whether the object's prototype chain contains an attribute, and the property must be a member defined by the object itself
</script>

The above methods are tested in IE-11.

So if we were to detect if an object in the prototype chain contained a specified property, I would have left the enumerable properties of the object to traverse.

<script>  //person  Object Properties for  var in p) {    + " <br> ");  }   // object property   for (var in o) {    + "<br>");  } </script>

The above is my judgment, confirming that the Showinfo attribute exists in the Person class property, and that the parent class does not have the custom attribute.

The above is my first blog, if there are any shortcomings, please kindly point out and pointing twos, common progress.

I am a rookie, I speak for myself!

The prototype of javascript-learning note-taking record

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.