-2

i want to know how in this obj when it declare and use 2 literal array they both reference each other

var o = {
    push:[].push,
    length:0,
    toString:[].join,
    valueOf:function(){
    return arguments[0] == "number" ? this.length : this.toString();
   }
};

o.push(1, 2, 3);

o.toString(); // "1,2,3"
(o*1).toString(); // 3

Nothing it is an fundamental i think

no need code

how 2 different literal array reference each other

ja0k0010
  • 7
  • 6
  • @T.J.Crowder Hi Thanks I corrected it! I just used to use backtick so ... - what do you mean by seventh undeleted question? – ja0k0010 Jan 07 '19 at 13:56
  • This is the seventh question listed on your profile. If you also wrote others but you've since deleted them (or the community has), I obviously wouldn't know about them. :-) – T.J. Crowder Jan 07 '19 at 13:58
  • Some one please take the negative rate out from my question – ja0k0010 Jan 07 '19 at 14:12

1 Answers1

0

The two arrays aren't referencing each other. In fact, neither of those arrays exists anymore once you've created o. You're just creating them, getting a reference to their push and join methods, and then throwing away the arrays. Those methods have no link back to the arrays you got them from.

The reason you see what you're seeing is that push is using o, not the arrays you got the methods from. (push works just fine if you call it on a non-array; most array methods do).

Here's a simpler situation demonstrating the same concept:

var obj1 = {
    setValue: function(value) {
        this.value = value;
    }
};
var obj2 = {
    setValue: obj1.setValue,
    showValue: function() {
        console.log(this.value);
    }
};

obj2.setValue(42);
obj2.showValue(); // 42

setValue sets the value on obj2, not obj1, because it was called as obj2.setValue(42).

More:

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • Look, i think i fully grasp the THIS concept. The example you provided is clear. Problem was that i didn't realized that push and join now freely do the same job in object literal as it is a array!!! That you mentioned it now " push is using o, not the arrays you got the methods from. (push works just fine if you call it on a non-array; most array methods do " – ja0k0010 Jan 07 '19 at 14:10
  • Btw, really these push and join do the same in object, it means they put values as properties in the object properties or just create a new array in object and work on that? – ja0k0010 Jan 07 '19 at 14:12
  • and more, the length property automatically increase to 3? not only 2, but 3? 1 2 3 -> 0 1 2 – ja0k0010 Jan 07 '19 at 14:16
  • @ja0k0010 - They actually do the work with properties on the object (`o`). Standard arrays in JavaScript [aren't really arrays at all](http://blog.niftysnippets.org/2011/01/myth-of-arrays.html) *(that's a link to a post on my blog)*, they're just objects that have a particular prototype and special behavior for a class of properties named in a particular way ("array indexes") and a special `length` property. – T.J. Crowder Jan 07 '19 at 14:16
  • @ja0k0010 - You pushed three entries into the object, so yes, its `length` is `3` (it wasn't *automatic*, `push` did that). – T.J. Crowder Jan 07 '19 at 14:17