prototypal-inheritance

Moving from `prototype` and `new` to a closure-and-exposure pattern

纵然是瞬间 提交于 2019-12-02 02:55:19
问题 I have been re-factoring someone else's JavaScript code. BEFORE: function SomeObj(flag) { var _private = true; this.flag = (flag) ? true : false; this.version="1.1 (prototype)"; if (!this._someProperty) this._init(); // leading underscore hints at what should be a 'private' to me this.reset(); // assumes reset has been added... } SomeObj.prototype.reset = function() { /* perform some actions */ } /* UPDATE */ SomeObj.prototype.getPrivate = function() { return _private; // will return

Override Methods with Prototypal Inheritance

亡梦爱人 提交于 2019-12-02 02:04:06
I'm using this clone method for prototypal inheritance from Pro JavaScript Design Patterns which is basically the same as Crockford's object() function . (The only difference is that Crockford adds invoke parens, but since F is empty I'm not sure that it matters. I don't think that the issue.) clone = function(object) { function F() {} F.prototype = object; return new F; }; So, applying this, I'm looking to make two objects, one which inherits methods from the other. One is for viewport dimensions and one is for device dimensions. But both use similar math comparisons so I think it makes sense

Moving from `prototype` and `new` to a closure-and-exposure pattern

感情迁移 提交于 2019-12-02 00:19:45
I have been re-factoring someone else's JavaScript code. BEFORE: function SomeObj(flag) { var _private = true; this.flag = (flag) ? true : false; this.version="1.1 (prototype)"; if (!this._someProperty) this._init(); // leading underscore hints at what should be a 'private' to me this.reset(); // assumes reset has been added... } SomeObj.prototype.reset = function() { /* perform some actions */ } /* UPDATE */ SomeObj.prototype.getPrivate = function() { return _private; // will return undefined } /* ...several other functions appended via `prototype`...*/ AFTER: var SomeObj = function (flag) {

instanceof check works on subclass without setting constructor [duplicate]

空扰寡人 提交于 2019-12-01 20:46:07
问题 This question already has answers here : What's the difference between using instanceof and checking the constructor? (2 answers) Closed 3 years ago . I have the following JavaScript code function Parent() { } function Child() { } Child.prototype = Object.create(Parent.prototype); Note the absence of the statement Child.prototype.constructor = Child; My understanding is that as the constructor property has not been set the instanceof checks should fail for new instances of Child class. var

How to create a JS object with the default prototype from an object without a prototype?

烂漫一生 提交于 2019-12-01 18:38:10
Background: The module query-string is for example able to parse key=value&hello=universe to an object {key: 'value', hello: 'universe'} . However, the module author has decided that the returned object does not have a prototype. In other words, this "bastard" object is created by Object.create(null) . Problem: It would be convenient to use parsed.hasOwnProperty('hello') but that is not possible without the default object prototype. Of course, one could Object.prototype.hasOwnProperty.call(parsed, 'hello') but I think we can all agree that such an expression is kill-immediately-after-birth

How do Chrome and Firefox print the object's class name in the console?

我怕爱的太早我们不能终老 提交于 2019-12-01 06:14:47
问题 If I create a Foo class using "traditional" Javascript classes, both chrome and Firefox will show the Foo name when printing Foo instances on the console: function Foo(){ this.x = 10; } console.log(new Foo()); // Foo {x: 10} On the other hand, if I use hand rolled prototypal inheritance then I don't get the helpful name when debugging function mkClass(init, proto){ return function(/**/){ var obj = Object.create(proto); init.apply(obj, arguments); return obj; } } var Bar = mkClass(function(){

Is it possible to determine if an object created with Object.create inherits from Array in JavaScript?

谁说胖子不能爱 提交于 2019-12-01 04:20:57
Identifying which objects are which is complicated in JavaScript, and figuring out which objects are arrays has something of a hacky solution . Fortunately, it manages to work in both of the following cases: Object.prototype.toString.call([]); // [object Array] Object.prototype.toString.call(new Array()); // [object Array] Great, no [object Object] in sight! Sadly, this method still manages to fail with this: var arr = Object.create(Array.prototype); Object.prototype.toString.call(arr); // [object Object] This is frustrating, so say the least. My arr object has all the methods of an array, it

Minor drawback with Crockford Prototypical Inheritance

爷,独闯天下 提交于 2019-11-30 19:19:04
Just experimenting with different inheritance techniques in JS, and came across something mildly discomfiting about Crockford's Prototypal Inheritance pattern: function object(o) { function F() {} F.prototype = o; return new F(); } var C, P = { foo:'bar', baz: function(){ alert("bang"); } } C = object(P); It's all good - except when you log to console - the object appears as F. I've seen classical emulation in which you can repoint the constructor - is there a similar method to coerce the objects (console) reference? The issue is that it's referring to the name of the constructor function.

Implement multiple inheritance in Javascript

别等时光非礼了梦想. 提交于 2019-11-30 16:19:59
I have observed that through the use of "prototype" property in Javascript (and indirectly setting up the prototype chain), one can implement multi-level inheritance in JavaScript. But is it possible to implement multiple inheritance in Javascript by some way? Any simple examples would be great. To implement simple inheritance you usually do MyClass.prototype = new MySuperClass(); but you could also copy the content of another "class" : MyClass.prototype = new MySuperClass(); var myOtherSuperClass = new MyOtherSuperClass(); for (var key in myOtherSuperClass) { MyClass.prototype[key] =

Why defining properties in the prototype is considered an antipattern

心已入冬 提交于 2019-11-30 14:11:16
问题 I often see this pattern to define javascript objects function Person(name) { this.name = name; } Person.prototype.describe = function () { return "Person called "+this.name; }; And in this article it says that adding properties directly to the prototype objct is considered an anti-pattern. Coming from "classical class based" languages, having to define the properties apart from methods doesn't sound quite right, moreoever in javascript, where a method should be just a property with a