setter / getter不适用于Number literal [英] setter/getter not working for Number literal

查看:82
本文介绍了setter / getter不适用于Number literal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为将Number.prototype的零填充扩展名重写为真正的getter / setter模式会很有趣。不是很难。我最终得到了这段代码( addSetter addGetter 只是使用 prototype .__ defineGetter__的包装函数/ __defineSetter __

I thougt it would be fun to rewrite my zero padding extension to the Number.prototype into a genuine getter/setter pattern. Not very difficult. I ended up with this code (addSetter and addGetter are just wrapper functions using prototype.__defineGetter__ / __defineSetter__.

Number.addSetter('leftPad0',
  function(v){
    var  len = (String(v).length - String(this).length)+1;
    this._leftPad0 = new Array(len).join('0')+this;
  }
);

Number.addGetter('leftPad0',
  function(){
    return this._leftPad0;
  }
);

适用于实数对象:

var a = new Number(977);
a.leftPad0 = 10000;
alert('a => '+a.leftPad0); // a => 00977

但不适用于数字文字:

var b = 977;
b.leftPad0 = 10000;
alert('b => '+b.leftPad0); // b => undefined

那么,不能到达设定者吗?或者如果到达二传手,不是它是一个数字?

So, doesn't b reach the setter? Or if it reaches the setter, isn't it a Number?

我在setter函数中将其记录在控制台中:

I logged this in the console from within the setter function:

this.constructor === Number // true
this instanceof Number //true

或者是没有达到吸气剂,或者达到它的时候,文字不是数字的实例?我在getter中记录了相同的内容。一切都很好也是正确的。

Or is the getter not reached, or when it's reached would the literal not be an instance of Number? I logged the same within the getter. All fine and true too.

那么,可能是因为我们无法使用数字文字来使用这种模式?或者我错过了什么?

So, what may be the reason that we are not able to use this pattern using a Number literal? Or have I missed something?

注意:如果我使用这个原型扩展('monkey patch'),就不会发生这种情况:

Note: this doesn't occur if I use this prototype extension ('monkey patch'):

Number.prototype.leftPad = function(base){
  var  len = (String(base).length - String(this).length)+1;
  return new Array(len).join('0')+this;
}

alert( (977).leftPad(10000) ); // 00977

[编辑]我仍然想知道是否需要致电这是一个错误,或者它是否符合/标准。无论如何,我现在为此设计了自己的对象:

[edit] I still wonder if we have to call this a bug, or if it's following the/a standard. Anyway, I deviced my own object for this now:

function NumPL(val,pval,chr){
   if (!(this instanceof NumPL)){
      return new NumPL(val,pval,chr);
   }
   this._value = new Number(val||0);
   this._padValue = pval || 10;
   this.chr = chr || '0';
}

NumPL.prototype = {
    get value(){
        return this._value;
    },
    set padValue(v) {
        this._padValue = v%10 === 0 ? v : 10;
    },
    set value(v) {
        this._value = v;
    },
    get padLeft(){
       var  len = (String(this._padValue).length - 
                   String(this._value).length)+1;
       return new Array(len).join(this.chr)+this._value;
    }
}
// Usage
var a = NumPL(977,10000);
alert(a.padLeft); //=> 00977
// or a real world example
var dat = new Date,
    datshort = [dat.getFullYear(),
                NumPL(dat.getMonth()+1).padLeft,
                NumPL(dat.getDate()).padLeft]
               .join('/');
 alert(datshort); //=> 2011/05/19


推荐答案

字面数字不是数字

typeof 3
// "number"

typeof new Number(3)
// "object"

因此没有原型可以修改数字文字。

So there is no prototype to modify for numeric literals.

但是,当您尝试在数字文字上设置属性时,看起来文字被包装在首先是Number对象。然后,在该对象上调用setter,这个将成为setter函数内的一个对象。然后,当setter返回并且这个展开将属性(在对象包装器上设置)放在后面时,文本被解包。

However, when you try to set a property on a numeric literal, it looks like the literal is wrapped in a Number object first. Then, the setter is called on that object and this will be an object inside the setter function. And then, the literal is unwrapped when the setter returns and this unwrapping leaves the property (which was set on the object wrapper) behind.

Number.prototype.__defineSetter__('leftPad0',
    function(v) {
        alert('In setter: ' + v + ' ' + (this instanceof Number));
        var len = (String(v).length - String(this).length) + 1;
        this._leftPad0 = new Array(len).join('0') + this;
    }
);

var a = new Number(977);
a.leftPad0 = 10000;
alert('a => ' + a._leftPad0);

var b = 977;
b.leftPad0 = 10000;
alert('b => ' + b._leftPad0);

http://jsfiddle.net/ambiguous/H77qk/2/

我在几个浏览器中收到了上述四个警报。

I get four alerts out of the above in several browsers.

很抱歉,我没有这种行为的权威参考,只是经验证据。

Sorry but I don't have an authoritative reference for this behavior, just empirical evidence.

这篇关于setter / getter不适用于Number literal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆