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)