Put a code First:
function O (user,pwd) {//use constructor
This.user=user;
This.pwd=pwd;
This.get=get;
return this;
}
function O2 (user,pwd) {//use factory
var obj=new Object ();
Obj.user=user;
Obj.pwd=pwd;
Obj.get=get;
return obj;
}
function O3 () {//use prototype
}
o3.prototype.user= ' abc ';
o3.prototype.pwd= ' dis ';
O3.propotype.get= ' get ';
O3.prototype.get () {
alert (THIS.PWD);
//}
function O4 (user,pwd) {
This.user=user;
This.pwd=pwd;
return this;
}
O4.prototype.get=function () {alert (' 123 ');}
function Get () {
Alert ("This User:" +this.user);
// }
function Test2 () {
var a=new O2 (' Us ', ' Pw '); Use Factory & Constructor
var a=new O3 (); Use prototype
A.get ();
var a=new O4 (' *u4 ', ' P4 '); Mixed
A.user= ' not ABC '; Set new property
alert (A.user);
A.get ();
}
The usual MS in this kind of, there may be other. Come and talk.
Digression: Yesterday hand owed, trying to use alert (window.appname) to the FF to see the browser version, the results popped unexpectedly is Netscape, I am not Firefox. Then ran to chrome under the test, another pop-up Netscape. Baidu search Netscape unexpectedly found JS is from Netscape company. Ashamed AH ashamed!!! Study for so long JS do not know Huang. So ran to find the genealogy, originally JS from Brendan Eich hand, 95 he created JS time, but also 31 years old. Ah, really white live, such as his general old me, to now can not learn JS, really people than popular dead. JS originally design time, did not think that they can from a phone call into a set of photos, internet, games, telephones in a smart machine. What a good fortune to get people!!! Perhaps the magic of each, even Brendan Eich himself did not think. It should be said that Brendan Eich created JS, and a large number of JS cattle people to achieve today so complex JS.
JS is not wood have class? It doesn't matter, they didn't design the prototype attribute?
JS is not a block of wood-level scope? It doesn't matter, people do not have the scope chain ~
How does JS implement member variable privatization? Oh, solve it with closures ~
Oh, so many basic concepts, completely dizzy, the road long its repair far-off.
To start with, this article discusses several ways to create JS objects, starting with the best understanding of the factory model:
Copy CodeThe code is as follows:
function Createperson (name,age,job) {
var o = {};
O.name = name;
O.age = age;
O.job = job;
O.sayname = function () {
alert (this.name);
};
return o;
}
var Tanya = Createperson ("Tanya", "a", "female");
var Ansel = Createperson ("Ansel", "a", "male");
Tanya.sayname ();
Ansel.sayname ();
This defines O as an empty object, and then sets a bunch of properties for O. In fact, you can give the O property directly, so it is OK if this is written.
Copy CodeThe code is as follows:
function Createperson (name,age,job) {
var o = {
Name:name,
Age:age,
Job:job,
Sayname:function () {
alert (this.name);
}
};
return o;
}
var Tanya = Createperson ("Tanya", "a", "female");
var Ansel = Createperson ("Ansel", "a", "male");
Tanya.sayname ();
Ansel.sayname ();
Another way is to use the invincible this, because this represents the object at the current runtime, the constructor of this is scoped to the new object, and the properties and methods of the currently running object are assigned to the new object, so that the object pattern is called the constructor pattern
Copy CodeThe code is as follows:
function Person (name,age,job) {
THIS.name = name;
This.age = age;
This.job = job;
This.sayname = function () {
alert (this.name);
};
}
var = new Person ("Tanya", "Tanya", "female");
var = new person ("Ansel", "Ansel", "male");
Tanya.sayname ();
Ansel.sayname ();
In this example, both Tanya and Ansel have a constructor property that points to person.
Consider the following scenario:
Copy CodeThe code is as follows:
function Person (name,age,job) {
THIS.name = name;
This.age = age;
This.job = job;
This.sayname = function () {
alert (this.name);
};
}
Person ("Tanya", "a", "female");
Person ("Ansel", "a", "male");
Window.sayname ();
Window.sayname ();
It was found that the two pop-up was Ansel, because without new, it was not an instance of the person, but simply the execution of the function. When a function is called at the global scope, this always points to the global object. The global object, in the browser, is the Window object.
We can also invoke the Sayname method in another object using the construct mode, remember apply and call, come to think of another situation,
Copy CodeThe code is as follows:
function Person (name,age,job) {
THIS.name = name;
This.age = age;
This.job = job;
This.sayname = function () {
alert (this.name);
};
}
var Olivia = {};
Person.call (Olivia, "Tanya", "a", "female");
Olivia.sayname ();
var Philip = {}
Person.apply (philip,["Ansel", "a", "male"]);
Philip.sayname ();
Prototype mode is to consider the prototype chain, analysis, Sayname method in the instance is repeated defined two times, but in fact there is no need to create two identical copies. Using the prototype method, you can make the Tanya and Ansel share a Sayname method.
So the prototype pattern is written as follows:
Copy CodeThe code is as follows:
function Person (name,age,job) {
THIS.name = name;
This.age = age;
This.job = job;
}
Person.prototype.sayname= function () {
alert (this.name);
};
var = new Person ("Tanya", "Tanya", "female");
var = new person ("Ansel", "Ansel", "male");
Tanya.sayname ();
Ansel.sayname ();
In practice, it is not immutable to apply a pattern, ingenious. When you need to share a method, use a prototype pattern, use a copy of the construction mode, you can combine all the information in the constructor, and by initializing the prototype in the constructor, the object maintains the advantages of using both constructors and prototypes.
Copy CodeThe code is as follows:
function Person (name,age,job) {
THIS.name = name;
This.age = age;
This.job = job;
if (typeof sayname! = "function") {
Person.prototype.sayname= function () {
alert (this.name);
};
}
}
var = new Person ("Tanya", "Tanya", "female");
var = new person ("Ansel", "Ansel", "male");
Ansel.sayname = function () {
Alert ("Hi Ansel, how hansome you are!");
}
Tanya.sayname ();
Ansel.sayname ();