您的位置:首页 > 其它

通过构造函数创建的对象的原型指向构造函数的prototype属性

2016-05-17 20:38 423 查看
<html>
<script>
function Range(from,to){
this.from = from;
this.to = to;
}

Range.prototype = {
includes:function(x){return x>=this.from && x<=this.to;},
forearch:function(){for(var i = 0;i<=5;i++) console.log(i);}
}

var range = new Range(1,5);
console.log(range.includes(3));
range.forearch();
console.log(range.from);

//在调用构造函数之前就已经创建了新对象,通过this关键字可以获得这个新的对象。Range()函数只不过是初始化
//this而已。
//对Range()构造函数的调用会自动使用Range.prototype作为Range对象的原型

</script>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: