<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>11-基于Object.create()实现类的继承</title>
</head>
<script>
function Parent(){
this.name="parent";
this.arr=[];
};
var parent=new Parent()
var son=Object.create(parent);//基于parent创建对象
son.name="son";
son.arr.push("one");
son.say=function(){
console.info(this.name);
};
console.info(son);
console.info(son.__proto__===parent);//true
//通过附加属性定义对象
var propertyDescriptor={
name:{
value:"daughter",
writable:true
},
age:{
value:10,
writable:true
}
};
var daughter=Object.create(parent,propertyDescriptor);
console.info(daughter);
</script>
<body>
</body>
</html>
javascript基础之十一(基于Object.create()实现类的继承)
最新推荐文章于 2022-04-28 10:12:26 发布