46   JS

一,Object.create 的实现

<html>

<body>

<script>

function person(name,age){
this.name = name;
this.age = age;
}

person.prototype.speak = function(word){
console.log(word)
}
person.prototype.run = function(){
console.log('run')
}

var one = Object.create(person.prototype);
console.log(one)
one.age;
one.name;
one.speak('hi');
one.run();

// Object.create 的实现
function createExtend(input){
var f = function () {}
f.prototype = input
var obj = new f();
return obj;
}

var two = createExtend(person.prototype);

console.log(two);
two.age;
two.name;
two.speak('hi');
two.run();
</script>
</body>

</html>

 

二,Object.create 做得事情

1,创建一个空对象

2,将该对象的 __proto__ 指向输入的第一个参数

3,返回该对象




Leave a Reply

Your email address will not be published. Required fields are marked *