Javascript, return a specific value instead of [Object Object] [x]

The question is in the header, but check out this code first:

function number(a) { return { add: function(b) { result = a + b; return this; }, substract(b) { result = a - b; return this; } } 

This code above is a simple example of a chain rule. I am reconfiguring the object, so I can do this continuously:

 number(2).add(5).add(3 * 12).substract(Math.random()); 

My problem is that I need to reconfigure the object so that the function is complete. I would like to change the chaining rule, but return a specific value. For example, number(2).add(3) will return 5.

Any suggestion is much appreciated.

Thanks to everyone in advanced form. [X]

+6
source share
3 answers

One way to make a numeric value, such as 5 "chainable," is to define a method on the corresponding prototype object, such as Number.prototype . For instance:

 Number.prototype.add = function (n) { return this + n } (5).add(2) // 7 5.0.add(2) // 7 5..add(2) // 7 ((5).add(2) + 1).add(34) // okay! 42 

The syntax above is ridiculous because 5.add(2) not valid: JavaScript expects a number (or "nothing") after 5. .. Since this is a global side effect (it will affect all numbers), unexpected interactions should be avoided.

Alternatively, <5> you can create a new Number object (5 is not a real instance of Number, although it uses Number.prototype!) And then copy the required methods. (I used to think this was the only way, but see KooiInc's answer - however, I'm not sure how correct it is to return a non-string from toString .)

 function ops(a) { return { add: function(b) { var res = new Number(a + b) // important! var op = ops(res) res.add = op.add // copy over singletons return res } } } function number(a) { return ops(a) } number(5).add(2) + 1 // 8 (number(5).add(2) + 1).add(34) // error! add is not a function 

However, remember that this introduces subtle issues:

 typeof 5 // number typeof new Number(5) // object 5 instanceof Number // false new Number(5) instanceof Number // true 

And so we need Number (SO search for "primitives" in JavaScript):

 x = 5 x.foo = "bar" x.foo // undefined 

Also, in combination with the cwolves answer, consider:

 function number (n) { if (this === window) { // or perhaps !(this instanceof number) return new number(n) } else { this.value = n } } 

Then both new number(2) and both number(2) will evaluate the new numbering object.

 number(2).value // 2 new number(2).value // 2 number(2) instanceof number // true new number(2) instanceof number // true 

Happy coding.

+6
source

You have two options. You can return new objects:

 function number(a){ return this instanceof number ? (this.value = a, this) : new number(a); } number.prototype = { valueOf : function(){ return this.value; }, add : function(b){ return new number(this.val + b); }, subtract : function(b){ return new number(this.val - b); } }; 

or you can modify the existing one (basically the same code as above, it's different):

 add : function(b){ this.value += b; return this; }, 

The difference is in how they work:

 var x = new number(5), y = x.add(10); // with first example // x == 5, y == 15 // with 2nd example // x == 15, y == 15, x === y 
+3
source

If you define the value as a property ( this.a ) and use toString in the returned object, you can bind the methods:

 function number(a) { return { a: Number(a) || 0, //if not a, or a===NaN, default = 0 add: function(b) { this.a += b; return this; }, subtract: function(b){ this.a -= b; return this; }, valueOf: function(){ return Number(this.a); }, toString: this.valueOf } } var n = number(5); alert(number.add(5).add(2).subtract(2)); //=> 10 alert(number.add(0.5)); //=> 10.5 alert(number(2).add(5).add(3 * 12).subtract(Math.random()); //=> 42.36072297706966 
+3
source

Source: https://habr.com/ru/post/891375/


All Articles