JS Base (prototype)

Source: Internet
Author: User

function Fun () {          alert (this. name);}; function person (name, age, gender) {          this. Name = name;            this. Age = Age ;           this. gender = gender;           // Add a method          to an object this. sayname = Fun ;}

Each time you create an object, a new Sayname method is created, and it is not the same, so it is completely unnecessary for all objects to share the same method

The Sayname method is defined globally, polluting the namespace of the global scope, and the definition is not secure in the global scope

Every function we create, the parser adds an attribute prototype to the function, which corresponds to an object, which is our prototype object.

If the function calls prototype as a normal function, when the function is called as a constructor, the object it creates has an implied property that points to the constructor's prototype object, which can be accessed through __proto__.

The prototype object is equivalent to a common area, and all instances of the same class can access the prototype object, and we can set the common content in the object to the prototype object

When we access a property or method of an object, it looks in the object itself, and if there is one, it is used directly, and if not, the prototype object is searched, and if found, it is used

When creating constructors, you can add properties or methods common to these objects uniformly to the constructor's prototype object, so that each object has these properties and methods

function person (name, age, gender) { This. Name =name;  This. Age =Age ;  This. Gender =gender; //add a method to an object//this.sayname = fun;}var Person=NewPerson (); Console.log (person.__proto__);//{constructor:?}Console.log (Person.prototype);//{constructor:?}Console.log (person.__proto__ = = Person.prototype);//true//person.sayname ();//ErrorPerson.prototype.sayName =function (argument) {alert ("Success");} var obj=NewPerson (); //obj.sayname ();console.log (obj); Console.log ("Sayname" in obj);//trueConsole.log (Obj.hasownproperty ("name"));//trueConsole.log (Obj.hasownproperty ("hasOwnProperty"));//falseConsole.log (Obj.__proto__.hasownproperty ("hasOwnProperty"));//falseConsole.log (Obj.__proto__.__proto__.hasownproperty ("hasOwnProperty"));//true

The prototype object is also an object, so it also has prototypes

When we use a property or method of an object, we look for it in ourselves, and if not, look for it in the prototype, and if not, go to the prototype and look for it until the object is returned undefined

When you print an object directly on the page, the output is actually the return value of the ToString () method of the object, and you can add a ToString ()

function person (name, age) {//body ...                     This. Name =name;  This. Age =Age ; } Person.prototype.toString=function () {return"Person[name=" + This. name+ ", age =" + This. age+ "']"; } var person=NewPerson ("Tom", 78); Person.tostring=function (argument) {return  This. name + "= =" + This. Age;          } console.log (person); Console.log (NewPerson ());

Garbage collection:

When an object does not have any variables or properties to reference it, at this point we will not be able to manipulate the object, at this time the object is a garbage

JS has an automatic garbage collection mechanism, which will automatically destroy the garbage, so we do not need to garbage collection, we have to do is no longer use of the object is assigned to NULL

JS Base (prototype)

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.