Prototype of js objects

Source: Internet
Author: User
// Prototype Method -- & gt; key prototype (an attribute owned by an Object, so all objects have this attribute) creates an Object // This method creates an Object and cannot pass parameters, this constructor is empty. FunctionDog () {} Dog. prototype. name & quot; small... SyntaxHi

// 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 ,"
");
}
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 ();


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.