On Wed, 04 Jan 2012 09:04:37 -0800, Krinkle krinklemail@gmail.com wrote:
On Wed, Jan 4, 2012 at 7:42 AM, Daniel Friesen lists@nadir-seen-fire.comwrote:
On Tue, 03 Jan 2012 06:14:36 -0800, Antoine Musso hashar+wmf@free.fr wrote:
Le Fri, 30 Dec 2011 18:31:30 +0100, Krinkle krinklemail@gmail.com a écrit:
Since virtually any value other than null and undefined is an object, including numbers, strings and functions.
Much like ruby! http://ruby-doc.org/core/Integer.html
$ irb
5.upto( 10 ) { |num| print "#{num}ber," }
5ber,6ber,7ber,8ber,9ber,10ber,=> 5
print 4.even?
true=> nil
You can change the 'even?' behavior to do something else of course :D
;) Oh no, in Ruby EVERYTHING is an object, there is no 'virtually' or 'almost'.
nil.class
=> NilClass
puts "nil is nil" if nil.nil?
nil is nil => nil
nil.is_a? NilClass
=> true
Although, their booleans are awkward.
true.class
=> TrueClass
false.class
=> FalseClass
true.class.superclass
=> Object
false.class.superclass
=> Object Last I checked the way to say "Is this a boolean?" in Ruby was `value === true || value === false`. Ugh.
In JavaScript we have Boolean instead.
-- ~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]
JavaScript:
true.constructor
< Boolean()
and Boolean inherits from Object too.
My note on booleans? I was pointing out ruby having nil an instance of NilClass. There's no object for undefined or null you can't use object syntax on it. I brought up booleans because ruby uses TrueClass and FalseClass instead of some sort of class like Boolean or BoolClass, and TrueClass and FalseClass don't have a common superclass besides Object which everything is derived from.
Difference though is that in constrary to Array and Object, JavaScript treats a literal strings, numbers and booleans always strictly equal to a similar one.
In that 5 === 5. Wheares new Number(5) === new Number(5) is false.
So eventhough numbers, strings and booleans are objects, they do not have "typeof" object, only those that are declared object through "typeof" are compared by reference.
So both [1, 2] === [1, 2] and new Array(1,2) === new Array(1,2) is false.
yay!
We call them primitives. The non-object versions of booleans, strings, numbers...
It's actually a little funkier than you think. In JS primitives are primitives, they aren't objects. Until you try to do something object like on them. Rather than 5 being a Number class instance it's actually a primitive 5 until you try to do something like (5).toString(); When you call a method on a primitive the primitive is actually temporarily converted into an object instance. Then discarded.
So if you try to do this: var i = 5; i.a = 'foo'; console.log(i.a);
You'll get undefined back. Because what's really happening here (well ignoring how js engines nowadays will try to eliminate unnecessary actions) is you're saying to have the 5 of i converted into a Number instance, the 'a' property set on that Number instance, and then that Number instance is discarded once it is set.
If you want a little insight into this, try running this: Number.prototype.foo = function() { console.log( typeof this ); }; (5).foo();
You'll find that even though 5 is a primitive that's typeof 'number' when called as a function the 'this' is actually an object instance of Number as if you had done new Number. And it's a new one each time: var arr = []; var i = 5; Number.prototype.a = function() { arr.push( this ); }; i.a(); i.a(); console.log( arr[0] === arr[1] );
Should come back false.