Re: [ClojureScript] Extending protocol to an existing JavaScript type

2015-09-07 Thread Thomas Heller
For a little JavaScript WTF and one reason why things are the way the are:

Number.prototype.whatAmI = function() { return typeof(this); };
var x = 1;
x.whatAmI();
"object"
typeof(x)
"number"

Fun times debugging that ...

Cheers,
/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.


[ClojureScript] Re: No regexp groups in Clojurescript clojure.string/replace

2015-09-07 Thread Lance Campbell
Okay, I played around a bit more with this problem over the long weekend and I 
found my groups. Running the following code:

(defn print-results
  [& rest]
  (println (str rest))
  "done")

(clojure.string/replace "one and two and three" #"(.+) and (.+) and (.+)" 
(partial print-results))

In the Clojure REPL I get:

(["one and two and three" "one" "two" "three"])
"done"

In the Clojurescript REPL I get:

("one and two and three" "one" "two" "three" 0 "one and two and three")
"done"

So, it appears that Clojure passes a single array argument whereas 
Clojurescript passes multiple arguments. So the code needs to be different for 
each language, but at least I found my groups :).

-- 
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

2015-09-07 Thread Thomas Heller


> The point I wanted to make is 'this' without 'new' will refer to the object 
> the function is defined on be it 'window' or whatever object.
> 

That is incorrect. "this" is the actual number, not window. new or not. 
Behavior is probably different for other types. ;)

Number.prototype.addFive = function() { return this + 5 };
var x = 1;
x.addFive(); => 6
var x = new Number(3)
x.addFive(); => 8

Anyways, we agree that JS is full of WTF. Let's enjoy our sweet sweet CLJS 
world. :)

Cheers,
/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

2015-09-07 Thread Marc Fawzi
:) Indeed ... different than the usual way, see below for how "new" changes
'this' from being the object whatAmI is defined on to

var T = function() { console.log('constructor called')}

T.prototype = {}

T.prototype.whatAmI = function() {
return {type: Object.prototype.toString.call(this), isEqualToPrototype:
(this === T.prototype)}
}

var x = new T  // (x is instance of T) prints constructor called

x instanceof T

true

x.whatAmI()
//prints: {type: "[object Object]", isEqualToPrototype: false}

var y = T  (y is T)

y instanceof T

false

y.prototype.whatAmI()

Object {type: "[object Object]", isEqualToPrototype: true}

See?

WTF indeed, in case of primitive types.

Maybe Brendan Eich can explain the primitive types stuff to me but last
time I mentioned ClojureScript on webapps mailing list he unleashed a huge
rant about how I should not speak of that which I don't understand. He was
clearly triggered by mention of CLJS. Seems like it has gotten under his
skin, especially with guys like Peter Hunt admitting that the React team
has been borrowing idea from CLJS. Microsoft lead on IE was far more
generous and open to discussing ideas around a thread safe version of the
DOM...



On Mon, Sep 7, 2015 at 2:55 PM, Thomas Heller  wrote:

>
>
> > The point I wanted to make is 'this' without 'new' will refer to the
> object the function is defined on be it 'window' or whatever object.
> >
>
> That is incorrect. "this" is the actual number, not window. new or not.
> Behavior is probably different for other types. ;)
>
> Number.prototype.addFive = function() { return this + 5 };
> var x = 1;
> x.addFive(); => 6
> var x = new Number(3)
> x.addFive(); => 8
>
> Anyways, we agree that JS is full of WTF. Let's enjoy our sweet sweet CLJS
> world. :)
>
> Cheers,
> /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] Re: No regexp groups in Clojurescript clojure.string/replace

2015-09-07 Thread Lance Campbell
Daniel,

You are correct!

I updated to version 1.7.122 and the problem goes away :).

Thanks for your help.

Lance.

-- 
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] Re: No regexp groups in Clojurescript clojure.string/replace

2015-09-07 Thread Daniel Compton
Which version of ClojureScript are you using Lance? It looks like this
difference is resolved in CLJS-1304
https://github.com/clojure/clojurescript/commit/3e146905999f9b9ffb249e139e266f845c4af34c
which
is part of CLJS 1.7.122 (not a stable release yet).


On Tue, Sep 8, 2015 at 6:02 AM Lance Campbell  wrote:

> Okay, I played around a bit more with this problem over the long weekend
> and I found my groups. Running the following code:
>
> (defn print-results
>   [& rest]
>   (println (str rest))
>   "done")
>
> (clojure.string/replace "one and two and three" #"(.+) and (.+) and (.+)"
> (partial print-results))
>
> In the Clojure REPL I get:
>
> (["one and two and three" "one" "two" "three"])
> "done"
>
> In the Clojurescript REPL I get:
>
> ("one and two and three" "one" "two" "three" 0 "one and two and three")
> "done"
>
> So, it appears that Clojure passes a single array argument whereas
> Clojurescript passes multiple arguments. So the code needs to be different
> for each language, but at least I found my groups :).
>
> --
> 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.
>
-- 
Daniel

-- 
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

2015-09-07 Thread Marc Fawzi
thomas,

<<
Number.prototype.whatAmI = function() { return typeof(this); };
var x = 1;
x.whatAmI();
"object"
typeof(x)
"number"
>>

var x = 1   is the same as ... var x = Number(1)

So in both cases x.whatAmI is going to refer to Number.prototype which is
indeed of type [object Number] or as typeof reports it as "object" while x
is going to refer to the primitive type returned by Number() or as typeof
reports it  as"number"

On the other hand, if you say:

var x = new Number(1) ... then the 'this' in whatAmI will refer to an
instance of Number which is also of type [object Number] which means
'typeof x' will be "object" not "number" and will be consistent (in its
String representation) with the output of x.whatAmI().

In this case the primitive type passed to the Number constructor (i.e. 1)
is going to be placed behind an accessor called 'valueOf()' so you can do
x.valueOf() to get 1

The point I wanted to make is 'this' without 'new' will refer to the object
the function is defined on be it 'window' or whatever object.

Intuitive, no? but I would not call this a WTF moment, although JS has so
many

:)




On Mon, Sep 7, 2015 at 4:55 AM, Thomas Heller  wrote:

> For a little JavaScript WTF and one reason why things are the way the are:
>
> Number.prototype.whatAmI = function() { return typeof(this); };
> var x = 1;
> x.whatAmI();
> "object"
> typeof(x)
> "number"
>
> Fun times debugging that ...
>
> Cheers,
> /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.