JavaScript creation Object (ii)

Source: Internet
Author: User
Tags define object constructor hasownproperty

Prototype mode: Each create a function has a prototype attribute, it is a pointer to an object;

How the prototype mode creates the function:

function Movie () {

};
Movie.prototype.name= "Interstellar";
movie.prototype.year=2014;
Movie.prototype.country= "American";
Movie.prototype.playmovie=function () {
alert (this.name);
}

var movie1=new Movie ();
Movie1.playmovie ();//interstellar;
var movie2=new Movie ();
Movie2.playmovie ();//interstellar;

alert (movie1.playmovie==movie2.playmovie);//true distinct from constructors

Object literal amount of way

function Movie () {

};
movie.prototype={
Name: "Interstellar",
year:2014,
Country: "American",
Playmovie:function () {
alert (this.name);
}
}
This way the Contructor property of the prototype object does not point to the movie, but to object.

Whenever the code reads a property, it searches for the object instance itself, does not continue to search for the prototype object that the pointer points to, and ends if it does; the hasOwnProperty () allows you to detect whether the property exists in the instance or in the prototype:

function Movie () {
};
Movie.prototype.name= "Fleetoftime";
movie.prototype.year=2014;
"Movie.prototype.country=";
Movie.prototype.playmovie=function () {
alert (this.name);
}
var movie1=new Movie ();
Alert (Movie1.hasownproperty ("name"));//False on the prototype
Movie1.name= "XXXX";
Alert (Movie1.hasownproperty ("name"));//overwrite the prototype and become true on the instance

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

var movie=new movie ();
Movie.prototype.playone=function () {
Alert ("one");
};
Movie.playone ()//"one"

Adds a method to the movie prototype object

But if you rewrite the entire prototype object, then you cut off the connection between the constructor and the original prototype:

function Movie () {

}

var movie=new moive ();
overriding a prototype object
movie.prototype={
constructor:movie,//object literal to force point to Movie, which would point to object
Name: "XXX",
year:2012,
Country: "XXXX",
Sayname:function () {}
}
The call to the Sayname () method will make an error
Movie.sayname ();//error

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

function Movie () {
}
movie.prototype={
Constructor:movie,
Name: "XXX",
year:2014,
"place:[", "Japan"],
Playname:function () {
alert (this.name);
}
}
var movie1=new Movie ();
var movie2=new Movie ();

Movie1.place.push ("Korea");

Alert (Movie1.place)//"China,japan,korea"

Alert (Movie2.place)//"China,japan,korea"

It was originally intended to change the place of movie1, and the result was changed to Movie2

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

function Movie (name,year,country) {
This.name=name;
This.year=year;
This.country=country
"this.place=[", "Japan"];
}
movie.prototype={
Constructor:movie,
Playname:function () {
alert (this.name);
}
}

var movie1=new Movie ("Interstellar", 2014, "American");
var movie2=new Movie ("Fleetoftime", 2014, "a");
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



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.