Continuing about the effect of "new" in JS since it is a subject of exploration/learning at this point, and I believe firmly in knowing your JS in order to master your CLJS, so maybe one day you could fix a bug in CLJS itself and be a contributor, which is hard to do in most cases if you don't know the underlying JS really well, and that's why I'm focusing on this point, so we understand the situation rather than dismiss it as immaterial.
See console session below explaining effect of "new" in your original example and follow up one. I don't think this tells you anything you didn't know, but the latter one shows how 'this' would refer to instance if you use "new" but not if you don't use new, and I thought primitive types would work the same but you brought up 'boxing' and I'm reading up on it now: http://www.jisaacks.com/javascript-boxing/ Where is the confusion as far as the effect of "new" in the examples below...? var MyClass = function() { /* ctor */ }; var inst = MyClass inst instanceof MyClass false <----- because no "new" var inst = new MyClass() inst instanceof MyClass true <----- because "new" And repeating the previous example in my original re-reply: var T = function() { console.log('constructor called')} T.prototype.whatAmI = function() { return {type: Object.prototype.toString.call(this), isEqualToPrototype: (this === T.prototype)} } T.whatAmI() var x = new T() // (x is instance of T) prints constructor called x instanceof T prints: constructor called prints: true x.whatAmI() prints: {type: "[object Object]", isEqualToPrototype: false} var x = T x.prototype.whatAmI() prints: {type: "[object Object]", isEqualToPrototype: true} <---- because 'this' in whatAmI now points to T.prototype So for sure it is confusing but all of the above is verified in console. What is 'boxing' btw? :) that's what I'd like to learn... On Tue, Sep 8, 2015 at 7:37 AM, Marc Fawzi <[email protected]> wrote: > Crossed context here. > > What I was pointing to is that trying to create an instance without "new" > means that 'this' in the prototype method whatAmI will point to the > prototype itself rather than the instance. That is what the === proves in > the example in my previous rely. I thought it was the problem in case of > primitive types too like Number. It seems unclear to you what I'm trying to > explain about the use of "new" to call the constructor vs assigning the > constructor by reference to some var. I am not sure what the confusion is > in my case aside from the primitive type case, so would love to learn if > there is actually some confusion about the effect of new! I think it's > important to understand how JavaScript works if you are programming in any > language that compiles to it. > > > > > Sent from my iPhone > > > On Sep 8, 2015, at 5:00 AM, Thomas Heller <[email protected]> wrote: > > > > Hmm I think you have those confused. > > > > Take the equivalent from Java: > > > > class MyClass { > > } > > > > MyClass x = new MyClass(); > > Class y = MyClass.class; > > > > or JS: > > > > var MyClass = function() { /* ctor */ }; > > var inst = new MyClass() > > > > inst != MyClass > > inst.prototype == MyClass > > > > inst is an instance of MyClass (prototype) but not equal to MyClass. > > > > What is going on with Numbers is related to boxing, so there is a > perfectly fine explanation but that doesn't make it less confusing when you > first encounter it. > > > > Enough of this, back to topic. Don't extend Native types. ;) > > > > /thomas > > > > -- > > Note that posts from new members are moderated - please be patient with > your first post. > > --- > > You received this message because you are subscribed to the Google > Groups "ClojureScript" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to [email protected]. > > To post to this group, send email to [email protected]. > > Visit this group at http://groups.google.com/group/clojurescript. > -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/clojurescript.
