JavaScript 继承
ES5
js继承之三(对象关联方式,Object.create())
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
//Object.create()
var a = {f:function(){console.log('hello a');},id:1};
var b = Object.create(a);
b.f();
ES6
ES6 Class 类
class Child extends Father { ... }
function A(name,age){
this.name=name;
this.age=age;
this.printA=printA;
function printA(){
console.log(name);
console.log(age);
}
}
A.prototype.printA2 = function(){
console.log('printA2');
}
function B(hobby,name,age){
this.hobby=hobby;
this.printB=printB;
function printB(){
console.log(hobby);
}
}
var a = new A('aa',18);
console.dir(a);
var b = new B('run','qw',16);
var c=Object.create(a);
b.__proto__=c.__proto__;
console.dir(b);
document.write(b.hobby);
b.printB();
b.printA();
b.printA2();
function _fuc(){
console.log('dsfsfsadsf');
}
function person(){
this.printObj = _fuc;
}
var A = new person();
console.dir(A);
本文深入探讨了JavaScript中实现继承的多种方式,包括ES5的Object.create()方法和ES6的class语法。通过实例展示了对象关联、原型链的构建及使用,适合希望深入了解JS内部机制的开发者。
9046

被折叠的 条评论
为什么被折叠?



