Javascript数字原型直接设置值 [英] Javascript Number Prototype Set Value Directly

查看:39
本文介绍了Javascript数字原型直接设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试直接通过原型方法设置一个数字. 通常,返回一个新值.

I try to set a number directly from a prototype method. Usually, a new value is returned.

this 也是一个对象.但我想不是参考. (?)

this of a number object, is also an object. But I guess not a reference. (?)

我有这个:

Number.prototype.bitSet = function(bit) {
    return this | (1<<bit);
};

但是想要这个:

Number.prototype.bitSet = function(bit) {
        this.value = this | (1<<bit);
    };

this.value是一个伪属性.因为这是数字的参考,而没有这个,您将覆盖它.但是问题是:这真的是对来源号码的引用吗?有可能这样做吗?将值直接分配给调用此方法的号码吗?

this.value is a pseudo property. Becuase this sould be a reference of the number and without that, you'll overwrite it. But the question is: Is this really a reference to the source number? Is it possible to do that? Assign the value directly to the number who called this method?

var num = 0; 
num.bitSet(9);
console.log(num); // num = 512

顺便说一句. chrome控制台会显示数字[[PrimitiveValue]].

Btw. chrome console prints [[PrimitiveValue]] for the number.

推荐答案

TL; DR-您无法做到这一点,bitSet的初始版本就是定义它的方式.使用它时,您需要保存其返回值,例如x = x.bitSet(2).不过,您可以根据需要创建自己的可变数字对象. (有关此内容的更多信息.)

TL;DR - You can't do that, your initial version of bitSet is how you need to define it. You'll need to save its return value when you use it, e.g., x = x.bitSet(2). You can create your own mutable number object, though, if you like. (More on that below.)

为清楚起见(您可能知道这一点):JavaScript同时具有编号 primitives Number objects .通常,您正在处理基本体. Number.prototype起作用的原因是,当在临时对象上调用方法时,将使用该原语的值创建一个临时对象.除非有明确地保存该对象的东西,否则就好像我们只是在处理基元一样.

Just for clarity (you probably know this): JavaScript has both number primitives and Number objects. Normally, you're dealing with primitives. The reason Number.prototype works is that a temporary object is created using the primitive's value when a method is called on it. Unless something explicitly saves the object, though, it's as though we were just dealing with primitives.

在JavaScript中数字是不可变的. 1 因此,您的bitSet方法无法更改其调用的数字值.相反,它必须返回更改后的新号码(例如您的原始版本).

Numbers are not mutable in JavaScript.1 So your bitSet method cannot change the numeric value of what it's called on; instead, it has to return a new number with the changes made (e.g., your original version).

请注意,即使可以更改Number对象的值,也几乎不会在分配给Number.prototype的函数之外的代码中处理数字对象.例如:

Note that even if you could change a Number object's value, you're almost never dealing with a number object in code outside functions you've assigned to Number.prototype. For instance:

Number.prototype.bitSet = function(bit) {
    return this | (1<<bit);
};
var x = 32;
x = x.bitSet(2);
console.log(x); // 36
console.log(typeof x); // "number", not "object"
var o = new Number(36);
console.log(typeof o); // "object"

在上面,当执行x = x.bitSet(2);时,将数字 primitive 转换为临时Number对象,调用您的bitSet方法,然后结果就是您的bitSet方法返回;除非bitSet进行某些操作以将this存储在某个位置,否则临时对象将被丢弃. (这是理论;实际上,如果JavaScript引擎可以确定函数中的代码仅使用该数字,就好像它是原始数字一样,则可以很好地完全优化该对象.)

In the above, when x = x.bitSet(2); is executed, the number primitive is converted to a temporary Number object, your bitSet method is called, and then the result is whatever your bitSet method returns; unless bitSet does something to store this somewhere, the temporary object is then thrown away. (That's the theory; in fact, your JavaScript engine may well optimize away the object entirely, if it can determine that the code in your function only uses the number as though it were a primitive number.)

因此,假设在上面的代码中,我们做了一些更改x.bitSet(2)行中Number对象的状态的操作.由于该对象是临时且未存储在任何地方(除非我们将其存储;它不在x中,x包含一个原始编号),所以我们在该对象上存储的任何内容都将丢失.我们甚至可以证明:

So suppose in my code above, we did something to change the state of the Number object in that x.bitSet(2) line. Since that object is temporary and not stored anywhere (unless we store it; it's not in x, x contains a primitive number), whatever we stored on the object would be lost. We can even prove that:

Number.prototype.test = function() {
  this.foo = Math.random();
  console.log("this.foo", this.foo); // some number
};
var x = 42;
x.test();
console.log(typeof x);       // "number", not "object"
console.log("x.foo", x.foo); // undefined

this绝对是对象,我们向其添加了一个属性并使用了该属性.但是x仍然具有原始语言.

this was definitely an object, we added a property to it and used that property. But x still had the primitive.

您可以设置自己的自己可变号码类型,

You could have your own mutable number type, though:

function MyNumber(value) {
    this.value = typeof value === "number" ? value : 0;
}
MyNumber.prototype.bitSet = function(bit) {
    this.value = this.value | (1 << bit);
};
MyNumber.prototype.valueOf = function() {
    return this.value;
};
MyNumber.prototype.toString = function() {
    return this.value.toString();
};

// Usage:
var m = new MyNumber(42);
m.bitSet(2);
console.log(String(m)); // "46"
var n = m + 5;
console.log(n);         // 51

只要JavaScript引擎需要将您的数字对象转换为数字,就会调用valueOf函数.当JavaScript引擎需要将您的数字对象转换为字符串时,会调用toString.

The valueOf function is called any time the JavaScript engine needs to convert your number object to a number. toString is called when the JavaScript engine needs to convert your number object to a string.

或在ES2015中:

class MyNumber {
    constructor(value) {
      this.value = typeof value === "number" ? value : 0;
    }
    bitSet(bit) {
      this.value = this.value | (1 << bit);
    }
    valueOf() {
      return this.value;
    }
    toString() {
      return this.value.toString();
    }
}

// Usage:
var m = new MyNumber(42);
m.bitSet(2);
console.log(String(m)); // "46"
var n = m + 5;
console.log(n); // 51

1 数字在JavaScript中不可更改" 从技术上讲,这是不正确的. 原始数字不是可变的,但是Number对象是–但是其基础数值(规范称为其[[NumberData]])无法更改. (Number对象可以具有状态可以更改的其他属性,而不能更改它们的数值.)因此,即使不是完全正确,数字在JavaScript中也不可变"是一个合理的简写.

1 "Numbers are not mutable in JavaScript" Technically, that's not true. Primitive numbers are not mutable, but Number objects are — but their underlying numeric value (what the spec calls its [[NumberData]]) cannot be changed. (Number objects can have other properties with state that can be changed, just not their numeric value.) So "Numbers are not mutable in JavaScript" is a reasonable shorthand statement, if not perfectly correct.

这篇关于Javascript数字原型直接设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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