JavaScript--ES5使用Object.create()继承

本文深入探讨了JavaScript中实现继承的多种方式,包括ES5的Object.create()方法和ES6的class语法。通过实例展示了对象关联、原型链的构建及使用,适合希望深入了解JS内部机制的开发者。

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);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值