prototype 的作用

vuqrzk5w 2014-02-13 10:49:00
// This is a constructor function that initializes new Range objects.
// Note that it does not create or return the object. It just initializes this.
function Range(from, to) {
// Store the start and end points (state) of this new range object.
// These are noninherited properties that are unique to this object.
this.from = from;
this.to = to;
}

// All Range objects inherit from this object.
// Note that the property name must be "prototype" for this to work.
Range.prototype = {
// Return true if x is in the range, false otherwise
// This method works for textual and Date ranges as well as numeric.
includes: function(x) { return this.from <= x && x <= this.to; },
// Invoke f once for each integer in the range.
// This method works only for numeric ranges.
foreach: function(f) {
for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
},
// Return a string representation of the range
toString: function() { return "(" + this.from + "..." + this.to + ")"; }
};


这里用Range.prototype = {...



// This is a constructor function that initializes new Range objects.
// Note that it does not create or return the object. It just initializes this.
function Range(from, to) {
// Store the start and end points (state) of this new range object.
// These are noninherited properties that are unique to this object.
this.from = from;
this.to = to;

// Return true if x is in the range, false otherwise
// This method works for textual and Date ranges as well as numeric.
includes: function(x) { return this.from <= x && x <= this.to; },
// Invoke f once for each integer in the range.
// This method works only for numeric ranges.
foreach: function(f) {
for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
},
// Return a string representation of the range
toString: function() { return "(" + this.from + "..." + this.to + ")"; }
}
;


有啥区别啊?
...全文
224 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyl8781697 2014-02-14
  • 打赏
  • 举报
回复
prototype 可以对 对象 添加属性和方法 你第二段代码是不正确的

		// This is a constructor function that initializes new Range objects.
// Note that it does not create or return the object. It just initializes this.
function Range(from, to) {
    // Store the start and end points (state) of this new range object.
    // These are noninherited properties that are unique to this object.
    this.from = from;
    this.to = to;
 
// Return true if x is in the range, false otherwise
    // This method works for textual and Date ranges as well as numeric.
    this.includes= function(x) { return this.from <= x && x <= this.to; };
    // Invoke f once for each integer in the range.
    // This method works only for numeric ranges.
    this.foreach= function(f) {
        for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
    },
    // Return a string representation of the range
    this.toString= function() { return "(" + this.from + "..." + this.to + ")"; };
};
Javascript Prototype
阿鱼 2014-02-14
  • 打赏
  • 举报
回复
街道向晚 2014-02-14
  • 打赏
  • 举报
回复
引用 5 楼 oN5GrzoN 的回复:
[quote=引用 4 楼 yyl8781697 的回复:] prototype 可以对 对象 添加属性和方法 你第二段代码是不正确的

		// This is a constructor function that initializes new Range objects.
// Note that it does not create or return the object. It just initializes this.
function Range(from, to) {
    // Store the start and end points (state) of this new range object.
    // These are noninherited properties that are unique to this object.
    this.from = from;
    this.to = to;
 
// Return true if x is in the range, false otherwise
    // This method works for textual and Date ranges as well as numeric.
    this.includes= function(x) { return this.from <= x && x <= this.to; };
    // Invoke f once for each integer in the range.
    // This method works only for numeric ranges.
    this.foreach= function(f) {
        for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
    },
    // Return a string representation of the range
    this.toString= function() { return "(" + this.from + "..." + this.to + ")"; };
};
Javascript Prototype
那,这样,和加在Prototype上,有啥区别啊?[/quote] 加prototype不同的对象,方法或参数共享同一个内存;直接在构造函数里面写不同的对象则调用不同的内存。
oN5GrzoN 2014-02-14
  • 打赏
  • 举报
回复
引用 4 楼 yyl8781697 的回复:
prototype 可以对 对象 添加属性和方法 你第二段代码是不正确的

		// This is a constructor function that initializes new Range objects.
// Note that it does not create or return the object. It just initializes this.
function Range(from, to) {
    // Store the start and end points (state) of this new range object.
    // These are noninherited properties that are unique to this object.
    this.from = from;
    this.to = to;
 
// Return true if x is in the range, false otherwise
    // This method works for textual and Date ranges as well as numeric.
    this.includes= function(x) { return this.from <= x && x <= this.to; };
    // Invoke f once for each integer in the range.
    // This method works only for numeric ranges.
    this.foreach= function(f) {
        for(var x = Math.ceil(this.from); x <= this.to; x++) f(x);
    },
    // Return a string representation of the range
    this.toString= function() { return "(" + this.from + "..." + this.to + ")"; };
};
Javascript Prototype
那,这样,和加在Prototype上,有啥区别啊?
似梦飞花 2014-02-13
  • 打赏
  • 举报
回复
你确定你那样写能正常运行?
zhjdg 2014-02-13
  • 打赏
  • 举报
回复
你第二段代码不报错吗?

87,909

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧