JavaScript creation Object (i)

Source: Internet
Author: User

Object definition: A collection of unordered properties that contain basic values, objects, functions, or a set of values that do not have a specific order.

The simplest way to create a custom object is to:

1 var movie=New  Object (); 2 movie.name= "Interstellar"; 3 movie.year=2014; 4 movie.country= "American"; 5 movie.playmovie=function() {6     alert (this. Name) ); 7 };
View Code

Of course, you can also write the object arguments as follows:

var movie={      Name: "Interstellar",      year:2014,      Country: "American",      playmovie:function () {        alert (this.name);}};

Both of these have obvious drawbacks: if you create many objects using the same interface, you generate a lot of duplicate code

So there is the engineering model, or the previous example to modify:

function Showmovie (name,year,country) {        var p=new Object ();        P.name=name;        P.year=year;        P.country=country;        P.playmovie=function () {           alert (this.name);};   return p;}

Assign a variable directly to it when you use it:

var Movie1=showmovie ("Interstellar", "American");

var Movie2=showmovie ("Fleetoftime", "China");

Factory mode solves the problem of creating multiple similar objects, but does not solve the problem of object recognition (how to know the type of an object).

So the constructor appears, and then the previous example modifies:

function Movie (name,year,country) {      this.name=name;
This.year=year; This.country=country; This.playmovie=function () { alert (this.name);};} Note://There is no return, no display of the created object, the method and properties are assigned to the This object;//The constructor should always start with a capital letter, but not a constructor with a lowercase start;

Here are a few ways to call constructors:

The first is through the new operator:

var movie1=new Movie ("Interstellar", "American");

var movie2=new Movie ("Fleetoftime", "The", "China");

Movie1, Movie2 all have a constructor attribute, point to Movie

alert (Movie1.constructor==movie);//truealert (Movie2.constructor==movie);//true;

Of course, the detection object type instanceof operator to be more reliable

Alert (movie1 instanceof Movie);//truealert (Movie2 instanceof Movie);//truealert (Movie1 instanceof Object);// Truealert (movie2 instanceof Object);//true

Note that all objects inherit from object;

The second invocation is to call it as a normal function, and then the properties and methods are added to the Window object, Isaac:

Movie ("Interstellar", "American"), Window.playname ();//"Interstellar"

The third is called by calling () (apply ()) to the scope of another object:

var p=new Object (); Movie.call (P, "Fleetoftime", "China");p. Playmovie ();//fleetoftime

These are the three ways to call the constructor, so what is the problem with the constructor, you can try to execute the following code to find that the same name function on different instances is unequal

Alert (Movie1.playmovie==movie2.playmovie)//false//constructor The main problem is that each method is recreated on each instance

We can solve this problem by moving the function definition out of the constructor, as follows:

function Movie (name,year,country) {      this.name=name;      This.year=year;      This.country=country;     This.playmovie=playmovie;} Funciton Playmovie () {    alert (this.name);} The Playmovie inside the constructor contains a pointer to the function

But if an object needs to define many methods, it needs to define many global functions. So there is the prototype pattern, of course, the prototype pattern will also have problems, the current general is the use of constructors + prototype combination method to create objects. See the next section.

Finally: December 13, 1937, invading the Japanese army brutally invaded Nanjing, creating a brutal massacre of Nanking, 300,000 of compatriots were killed, countless women were ravaged, countless children died, one-third buildings were destroyed, a large number of property was looted. The dehumanizing massacre, which was made by the Japanese Army, was one of the "three great tragedies" in the history of the Second World War, a shocking anti-human crime, a very dark page in human history, not forgetting the national humiliation.

  

  

 

  

  

JavaScript creation Object (i)

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.