<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>
通过构造函数创建的对象的原型指向构造函数的prototype属性
最新推荐文章于 2024-07-05 10:35:50 发布