Copy Code code as follows:
var people={name: "Xiong", age:15};
var person=function (user,age) {
This.name=user;
This.age=age;
This.say=function () {alert ("I am" +this.name+ "\ n" +this.age);}
//}
var chairman=function (name,salary) {
Person.call (This,name);
// }
var bill=new person ("Bill", 15);
var hu=new chairman ("Hu Jintao");
Person.prototype.eat=function () {
Alert ("I ' m eating");
// }
Bill.eat ();
function person (name)//base class constructor
{
THIS.name = name;
};
Person.prototype.SayHello = function ()//prototype Add method to the base class constructor
{
Alert ("Hello, I ' m" + this.name);
};
function Employee (name, salary)//subclass Constructor
{
Person.call (this, name); Call base class constructor
This.salary = salary;
};
function Xiong (name,age) {
Employee.call (This,name);
}
Employee.prototype = new Person (); It's interesting to build a base class object as a prototype of a subclass prototype.
Xiong.prototype=new Employee ();
Employee.prototype.ShowMeTheMoney = function ()//prototype Add method to subclass constructor
{
Alert (THIS.name + "$" + this.salary);
};
var billgates = new Person ("Bill Gates"); Create a BillGates object for the base class person
var stevejobs = new Employee ("Steve Jobs", 1234); To create a Stevejobs object for child class Employee
var hiakuotiankong=new xiong ("brighter");
var benbenxiong=new xiong ("Stupid Bear");
Billgates.sayhello (); A method called directly to prototype through an object
Hiakuotiankong. SayHello (); Call the base class prototype method directly through the subclass object, pay attention!
Benbenxiong. Sayhello=function () {//Masking the SayHello method of the prototype
Alert ("Haha,i ' M" +this.name);
}
Benbenxiong. SayHello ();
Stevejobs.showmethemoney (); A method of directly calling subclass prototype by subclass Object
Alert (Billgates.sayhello = = Stevejobs.sayhello); Display: True to indicate that the method of prototype is shared
Xiong.prototype.goodbye=function () {
Alert (this.name+ "Bye-bye");
}
Benbenxiong. Goodbye ();
In JavaScript, prototype not only allows the object to share his own wealth, but prototype also has a root to ask his ancestors
Nature, so that the legacy of their ancestors can be passed down from generation to generations. When you read a property from an object or call a method, if the object is
If there is no such property or method, it will go to the prototype object of its own association, and if prototype does not, it will
Go to prototype's own associated predecessor prototype to find it until it is found or traced back to the end of the process.
Within JavaScript, the object's properties and method traceability mechanisms are implemented through so-called prototype chains. When you use new
operator, the prototype object of the constructor is also assigned to the newly created object, which is built into the original
Type Object. Objects built into the prototype object should be externally invisible, although some browsers (such as Firefox) can let us access this
Built-in prototype objects, but this is not recommended. The built-in prototype object itself is also an object and has its own associated prototype object, which
Formed the so-called prototype chain.
At the very end of the prototype chain is the prototype object that the object constructor prototype attribute points to. This prototype object
Is the oldest ancestor of all objects, and this ancestor has achieved the innate method of all objects such as toString. Other built-in
constructors, such as function, Boolean, String, Date, and REGEXP, are prototype from this ancestor.
, but they each define their own attributes and methods, so that their descendants show the characteristics of their clans.