<<
var MyClass = function() { /* ctor */ };
var inst = new MyClass()
inst.prototype == MyClass
>>
"inst is an instance of MyClass (prototype) but not equal to MyClass."
Yes, no one argues with that. It is an instance of MyClass and it is
obviously !== MyClass,
But saying 'inst.prototype == MyClass' is not correct.
You've left it without comment so I just want to be sure it does not
confuse anyone.
*"Boxing" is the real take for me fro this thread, and it shines a light on
how to work with primitive types.*
The rest of it from my side can be summarized as follows:
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
....
If there's anything you think I'm confused about (aside from Boxing, which
I've just learned about thanks to you!) please don't shy away from
explaining.
I've always believed in the constructivist approach to programming, i.e. to
understand things from the very bottom, and in this case it is the JS stuff
that is at the very bottom of CLJS...
On Tue, 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.