// Prototype --> key: prototype (an attribute of an Object, so all objects have this attribute). Create an Object.
// When an object is created in this way, parameters cannot be passed because the constructor body is empty.
Function Dog (){}
Dog. prototype. name = "Tom"; // The attribute value can also be an array.
Dog. prototype. age = 21;
Dog. prototype. get = function (name, age)
{
This. name = name;
This. age = age;
Document. write ("name:" + this. name + "," + "age:" + this. age );
}
// New Dog (). get ("Beast", 24 );
// Create an object using a prototype
Function Cat (){}
Cat. prototype. name = new Array ();
Cat. prototype. age = 3;
Cat. prototype. get = function ()
{
Document. write ("name:" + this. name + "," + "age:" + this. age, "<br/> ");
}
Var cat1 = new Cat ();
Cat1.name. push ("James ");
Cat1.name. push ("Li Si ");
Cat1.age = 1;
Cat1.get ();
Var cat2 = new Cat ();
Cat2.get ();
// Age is a prototype of Number. Its value is only a copy of it and does not change its province. But name is of the array type, it is of the reference type, so its value will change its province.
// Construct an object using the prototype and constructor Method
// Benefit: the methods in the original object can be shared, and the object's inaccessible attributes can be created through the constructor.
Function Person (name, age)
{
This. name = name;
This. age = age;
}
Person. prototype. get = function ()
{
Document. write ("name:" + this. name + "," + "age:" + this. age );
}
// New Person ("Cao Huan", 21). get ();
// Dynamic prototype construction object
// The property value of the constructed object can be changed, but the method is instantiated only once (determined by the flag)
Function Dog (name, age)
{
// Static sex = "male ";
This. name = name;
This. age = age;
// Alert (typeof Dog. asd = 'undefined ');
If (typeof Dog. flag = "undefined ")
{
Alert ("value executed once ");
Dog. prototype. get = function ()
{
Document. write ("name:" + this. name + ", age" + this. age );
}
Dog. flag = true;
// Alert (Dog. flag );
}
}
Var d1 = new Dog ("Xiao Zi", 21 );
Var d2 = new Dog ("Xiaohua", 22 );
// D1.get ();
// D2.get ();