pouët.net

jsbin

category: code [glöplog]
 
This is a thread for posting random JavaScript-snippets that may or may not do something interesting. It was created due to an unexplainable urge of mine to post this silly shit:

Code:<script> // This script will always be 20 years behind the state of the art. var init = function () { var d = new Date(); var past = d.getFullYear() - 20; document.body.appendChild(document.createTextNode('Hi! ' + past + ' called and wanted its stupid JavaScript back.')); }; // Commence stupidity window.onload = init; </script>


Live demo
added on the 2015-11-23 20:55:10 by El Topo El Topo
Quote:
Array(10).join(1/0)
You never know when you need to write Infinity 10 times.
added on the 2015-11-23 21:40:19 by mog mog
Code: var g = {}; function F () { return g; } console.log("new F() instanceof F -> ", new F() instanceof F); console.log("g === new F() -> ", g === new F());
Code:new F() instanceof F -> false _display:36:1 g === new F() -> true
added on the 2015-11-24 13:14:18 by sol_hsa sol_hsa
ignoring the paste mistake, I kinda expected that result, because it's unexpected. Why does it happen? =)
added on the 2015-11-24 13:15:11 by sol_hsa sol_hsa
because of the empty object {} being returned. guess it assumes a constructor would not return an empty object.
added on the 2015-11-24 14:14:28 by psenough psenough
The JavaScript spec says that if an object is returned from a constructor it should use that as the result of the new operation. Otherwise, it should use the current this value. So if you return this, a primitive, null, or undefined things will behave as is usually expected.

I have only used this trick once, as IE8 only supported defineProperty on DOM elements. Others supported it on any object. So in IE9+ and other browsers, I defined the properties on this and returned it. In IE8 I defined them on a DOM element I created with an invalid tag name and returned that.

I've not found any other uses, though if you wanted a singleton hack that might be one way to do it.

I think this behavior is defined in section 11.2.2 and 8.7.1 but I'm not entirely certain.
Quote:
Quote:
Array(10).join(1/0)
You never know when you need to write Infinity 10 times.

Wow, division by zero. Won't that destroy the universe ten times over or something? ;)
added on the 2015-12-02 11:37:13 by El Topo El Topo
Code:Array(10).forEach(function (_, i) { console.log(i); });
Prints nothing, but as you can see from the previously mentioned join, you do get the separator repeated 10 times. The specification sheds light into this oddity by indicating the provided function is only called for existing elements: those for which [[HasProperty]] returns true. The result of "Array(10)" is indeed an array of length 10, but none of its indexes have been assigned to by any means.

To get the desired effect, a call to the newer "fill(...)" is required:
Code:Array(10).fill(0).forEach(function (_, i) { console.log(i); });
Though if you wanted the Ruby construct, that is a bit different:
Code:Number.prototype.times = function (callback) { for (var i = 0; i < this; i++) callback(i); }; (3).times(i => console.log(i));


JavaScript will complain "SyntaxError: identifier starts immediately after numeric literal" if you try to call the function directly on the 3 but has no such problems if you wrap it as above, or call it on a variable that was assigned to 3.

login