js继承

一.prototype(原型)

1.

function Person (name) {

this.name = name;

}

function Star (name) {

}   

Star.prototype = new Person("鹿晗");

var who = new Star();

console.log(who.name);

2.

function Person (name) {}

Person.prototype.name = "鹿晗";

function Star (name) {}   

Star.prototype = Person.prototype;

var who = new Star();

console.log(who.name);

二.call(一个字符)

function Person (name) {

this.name = name;

}

function Star (name) {

Person.call(this,name);

}                                                                                          

var who = new Star("鹿晗");

console.log(who.name);

三.apply(一个数组)

function Person (name) {

this.name = name;

}

function Star (name) {

Person.apply(this,[name]);

}                                                                                          

var who = new Star("鹿晗");

console.log(who.name);

posted on 2016-08-17 20:43  rainbow727  阅读(249)  评论(0编辑  收藏  举报