//定义类的构造函数
function Person(name) {
this.name = name || '默认姓名';
}
//定义该类所有实例的公共方法
Person.prototype.getName = function() {
return this.name;
}
var smith = new Person('Smith');
var jacky = new Person('Jacky');
console.log( smith.getName(), jacky.getName() ); //Smith Jacky
//定义类的构造函数
function Person(name) {
this.name = name || '默认姓名';
}
//定义该类所有实例的公共方法
Person.prototype.getName = function() {
return this.name;
}
function Author(name, books) {
//继承父类构造函数中定义的属性
//通过改变父类构造函数的执行上下文来继承
Person.call(this, name);
this.books = books;
}
//继承父类对应的方法
Author.prototype = new Person(); //Author.prototype.constructor === Person
Author.prototype.constructor = Author; //修正修改原型链时造成的constructor丢失
Author.prototype.getBooks = function() {
return this.books;
};
//测试
var smith = new Person('Smith');
var jacky = new Author('Jacky', ['BookA', 'BookB']);
console.log(smith.getName()); //Smith
console.log(jacky.getName()); //Jacky
console.log(jacky.getBooks().join(', ')); //BookA, BookB
console.log(smith.getBooks().join(', ')); //Uncaught TypeError: smith.getBooks is not a function
Author.prototype = (function() {
function F() {}
F.prototype = Person.prototype;
return new F();
})();
/*继承方法的函数*/
function extend(son, father) {
function F() {}
F.prototype = father.prototype;
son.prototype = new F();
son.prototype.constructor = son;
}
//A类
function A() {
console.log('A()');
}
A.prototype.hello = function() {
console.log('Hello, world.');
}
//B类
function B() {
A.call(this);
console.log('B()');
}
extend(B, A);
//C类
function C() {
B.call(this);
console.log('C()');
}
extend(C, B);
//D类
function D() {
C.call(this);
console.log('D()');
}
extend(D, C);
//E类
function E() {
D.call(this);
console.log('E()');
}
extend(E, D);
//创建一个E的实例
var e = new E(); //A() B() C() D() E()
e.hello(); //hello, world.
//这个函数可以理解为克隆一个对象
function clone(object) {
function F() {}
F.prototype = object;
return new F();
}
var Person = {
name: 'Default Name';
getName: function() {
return this.name;
}
}
//接下来让Author变为Person的克隆体
var Author = clone(Person);
//接下来让Author变为Person的克隆体
var Author = clone(Person);
Author.books = [];
Author.getBooks = function() {
return this.books.join(', ');
}
//增加一个作者Smith
var Smith = clone(Author);
console.log(Smith.getName(), Smith.getBooks()); //Default Name
Smith.name = 'Smith';
Smith.books.push('<<Book A>>', '<<Book B>>'); //作者写了两本书
console.log(Smith.getName(), Smith.getBooks()); //Smith <<Book A>>, <<Book B>>
//再增加一个作者Jacky
var Jacky = clone(Author);
console.log(Jacky.getName(), Jacky.getBooks()); // Default Name <<Book A>>, <<Book B>>
//这个函数可以理解为克隆一个对象
function clone(object) {
function F() {}
F.prototype = object;
return new F();
}
var Person = {
name: 'Default Name',
getName: function() {
return this.name;
}
}
//接下来让Author变为Person的克隆体
var Author = clone(Person);
Author.books = [];
Author.getBooks = function() {
return this.books.join(', ');
}
//增加一个作者Smith
var Smith = clone(Author);
Smith.name = 'Smith';
Smith.books = [];
Smith.books.push('<<Book A>>', '<<Book B>>'); //作者写了两本书
console.log(Smith.getName(), Smith.getBooks()); //Smith <<Book A>>, <<Book B>>
//再增加一个作者Jacky
var Jacky = clone(Author);
Jacky.name = 'Jacky';
Jacky.books = [];
Jacky.books.push('<<Book C>>', '<<Book D>>');
console.log(Jacky.getName(), Jacky.getBooks()); // Jacky <<Book C>>, <<Book D>>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有