JavaScript creation Object (ii)

Source: Internet
Author: User
Tags hasownproperty

Prototype mode: Each creation of a function has a prototype property, which is a pointer to an object;

How the prototype mode creates the function:

1 functionMovie () {2 3 };4Movie.prototype.name= "Interstellar";5movie.prototype.year=2014;6movie.prototype.country= "American";7Movie.prototype.playmovie=function(){8Alert This. Name);9 }Ten  One varmovie1=NewMovie (); AMovie1.playmovie ();//Interstellar; - varMovie2=NewMovie (); -Movie2.playmovie ();//Interstellar; the  -alert (Movie1.playmovie==movie2.playmovie);//true differs from constructor function
View Code

How object literals are

1 functionMovie () {2 3 };4Movie.prototype={5Name: "Interstellar",6year:2014,7Country: "American",8Playmovie:function(){9Alert This. Name);Ten } One } A //in this way, the Contructor property of the prototype object does not point to the movie, but instead points to object. 
View Code

Whenever the code reads a property, it searches for the object instance itself, does not continue to search for the prototype object pointed to by the pointer, and if so, ends with hasOwnProperty () to detect whether the attribute is present in the instance or in the prototype:

1 functionMovie () {2 };3Movie.prototype.name= "Fleetoftime";4movie.prototype.year=2014;5movie.prototype.country= "China";6Movie.prototype.playmovie=function(){7Alert This. Name);8 }9 varmovie1=NewMovie ();TenAlert (Movie1.hasownproperty ("name"));//false on the prototype OneMovie1.name= "XXXX"; AAlert (Movie1.hasownproperty ("name"));//overwrite the prototype and turn it into true on the instance
View Code

The dynamics of the prototype: any modifications made to the prototype object can be immediately reflected from the instance, for example:

1 var movie=new  Movie (); 2 movie.prototype.playone=function() {3     alert ("one"); 4 }; 5 movie.playone (); // "One" 6 7 // added a method to the movie prototype object
View Code

However, if the entire prototype object is rewritten, then the connection between the constructor and the original prototype is cut off:

1 functionMovie () {2 3 }4 5 varMovie=Newmoive ();6 //overriding a prototype object7Movie.prototype={8Constructor:movie,//object literal is forced to point to the movie, which originally points to object9Name: "XXX",Tenyear:2012, OneCountry: "XXXX", ASayname:function(){} - } - //An error occurs when calling the Sayname () method theMovie.sayname ();//Error
View Code

Problem with prototype objects: There is a big problem with attributes that contain reference type values, as in the following example

1 functionMovie () {2 }3Movie.prototype={4 Constructor:movie,5Name: "XXX",6year:2014,7place:["China", "Japan"],8Playname:function(){9Alert This. Name);Ten } One } A varmovie1=NewMovie (); - varMovie2=NewMovie (); -  theMovie1.place.push ("Korea"); -  -Alert (Movie1.place)//"China,japan,korea" -  +Alert (Movie2.place)//"China,japan,korea" -  + //It was originally intended to change Movie1 's place, and the result was changed to Movie2 .
View Code

The best way to create objects: Constructors to define instance properties, prototype patterns to define methods and shared properties;

1 functionMovie (name,year,country) {2       This. name=name;3       This. year=Year ;4       This. country=Country5       This. place=["China", "Japan"];6 }7Movie.prototype={8 Constructor:movie,9Playname:function(){TenAlert This. Name); One } A } -  - varmovie1=NewMovie ("Interstellar", "American"); the varMovie2=NewMovie ("Fleetoftime", "The", "China"); -Movie1.place.push ("Korea"); -alert (movie1.place);//"China,japan,korea" -alert (movie2.place);//"China,japan" +  -alert (movie1.place==movie2.place);//false +alert (movie1.playname==movie2.playname);//true
View Code

JavaScript creation Object (ii)

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.