3 ms·
Awesome. I do notice a gigantic warning on MDN to not rely on this behavior, though: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Funct
by jayferd 15y ago
Awesome. I do notice a gigantic warning on MDN to not rely on this behavior, though: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind#section_9 https://developer.mozilla.org/en/JavaScript/Reference/Global...
In p.js (github.com/jayferd/pjs), I used this pattern/workaround:
var Breakfast = function(args) {
if (!(this instanceof Breakfast)) return new Breakfast(arguments);
if (args && typeof this.init === 'function') this.init.apply(this, args);
}
So these would be equivalent:
Breakfast('bacon', 'eggs')
new Breakfast(['bacon', 'eggs'])
Whereas `new Breakfast` would return an "uninitialized" object as in Object.create.
- eldude 15y agoI used a similar pattern once, except _generically_ as follows: function Breakfast(my, args) { if (!(this instanceof arguments.callee)) return new arguments.callee(arguments); if (Object.prototype.toString.call(arguments[0]) === '[object Arguments]') arguments.callee.apply(this, args); }