<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>prototypeInherit</title>
<script type="text/javascript">
//原型式继承:实质上就是类式继承的函数封装(但有缺陷)
function inheritObject(obj){
//声明一个过渡函数对象
function _f(){}
_f.prototype = obj;
return new _f();
}
var book = {
name:'js book',
alike:['css','html']
}
var newbook = inheritObject(book);
newbook.name = 'ajax book';
console.log(newbook.name); //'ajax book'
newbook.alike.push('js');
console.log(newbook.alike); //["css", "html", "js"]
var combook = inheritObject(book);
console.log(combook.name); //'js book'
console.log(combook.alike); //["css", "html", "js"]newbook中修改加入的'js'元素,combook中也共享了
//小结:原型继承中——父对象的直接属性被分别拷贝,数组类结构属性会被共用。(推荐使用类式继承,继承的比较完美,每个实例都是独立对象,又都继承了父类的直接属性和原型prototype属性)
//本例已经通过验证
</script>
</head>
<body>
</body>
</html>