Re: [ClojureScript] Extending protocol to an existing JavaScript type
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 clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.
Re: [ClojureScript] Extending protocol to an existing JavaScript type
<< Your code has this in it: Object.prototype.toString.call(this) which is something you usually do not do. The first argument to the .call function becomes "this" in the context of the function which is "cheating" and bypassing prototype inheritance. I consider .call equal to reflection in Java. You should be doing this.toString(). >> This is because some primitive types do not support toString undefined.toString() VM188:2 Uncaught TypeError: Cannot read property 'toString' of undefined at :2:10 at Object.InjectedScript._evaluateOn (:895:140) at Object.InjectedScript._evaluateAndWrap (:828:34) at Object.InjectedScript.evaluate (:694:21)(anonymous function) @ VM188:2InjectedScript._evaluateOn @ VM34:895InjectedScript._evaluateAndWrap @ VM34:828InjectedScript.evaluate @ VM34:694 Object.prototype.toString.call(undefined) "[object Undefined]" So it becomes a rule of thumb to use Object.prototype.toString rather than call .toString on the object and sometimes have JS throw an exception See this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString On Tue, Sep 8, 2015 at 9:45 AM, Thomas Hellerwrote: > > > > > var x = T > > x.prototype.whatAmI() > > prints: {type: "[object Object]", isEqualToPrototype: true} < > because 'this' in whatAmI now points to T.prototype > > > > The thing is that you are not supposed to call methods on the prototype > directly. > > Your code has this in it: > > Object.prototype.toString.call(this) > > which is something you usually do not do. The first argument to the .call > function becomes "this" in the context of the function which is "cheating" > and bypassing prototype inheritance. I consider .call equal to reflection > in Java. You should be doing this.toString(). > > I cannot explain prototype inheritance well enough and will probably only > cause more confusion at this point. [1] does a way better job than I ever > could. > > Cheers, > /thomas > > [1] > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain > > -- > 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 clojurescript+unsubscr...@googlegroups.com. > To post to this group, send email to clojurescript@googlegroups.com. > 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 clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.
[ClojureScript] Use a specialized Clojure library in Clojurescript
Is it always necessary to rewrite a Clojure library in Clojurescript, in order to use its functions? There are several third-party Clojure libraries that I'd like to use in a web page, but cannot find a way to call the Jar methods directly. -- 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 clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.
Re: [ClojureScript] Use a specialized Clojure library in Clojurescript
This is a good question. Until very recently, there was no easy way to share code between Clojure and ClojureScript at all, although the pure clojure-core stuff did work pretty much out of the box. Still it's quite common that a library has dependencies to other jvm-stuff, or uses jvm-stuff under the hood. Or, the horror, calls external processes. If this is the case your out of luck/has to convert the jvm-specific things to something availiable in cljs/js. What libraries are you thinking of? /Linus 2015-09-09 0:09 GMT+02:00 Dan Campbell: > Is it always necessary to rewrite a Clojure library in Clojurescript, in > order to use its functions? > > There are several third-party Clojure libraries that I'd like to use in a web > page, but cannot find a way to call the Jar methods directly. > > > > -- > 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 clojurescript+unsubscr...@googlegroups.com. > To post to this group, send email to clojurescript@googlegroups.com. > 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 clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.
[ClojureScript] Clojurescript REPL within PDF
Need a Clojurescript REPL? Too lazy to open planck? Use a PDF instead. NB: Only works in Foxit Viewer (Windows) or Chrome. Adobe and Preview blow the stack. -- 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 clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.
Re: [ClojureScript] Extending protocol to an existing JavaScript type
<< 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 Hellerwrote: > 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 clojurescript+unsubscr...@googlegroups.com. > To post to this group, send email to clojurescript@googlegroups.com. > 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 clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.
Re: [ClojureScript] Extending protocol to an existing JavaScript type
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 Hellerwrote: > > 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 clojurescript+unsubscr...@googlegroups.com. > To post to this group, send email to clojurescript@googlegroups.com. > 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 clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.