prototypal-inheritance

Role of 'prototype' property amidst design of `function` type object?

人走茶凉 提交于 2020-01-03 05:09:22
问题 As per the understanding, The main purpose for the existence of prototype property in a function type object is to allow properties/methods sitting under prototype to get inherited by other objects. This enables prototypical inheritance. Considering window['Number'] function type object, In general, Idea is to understand the thought process on what comes under prototype . So. I would like to take a specific example i.e., Number , with below questions. From design perspective, how would I

What does a JS function's prototype property used for?

核能气质少年 提交于 2020-01-02 09:29:35
问题 I understand javascript prototype inheritance through the __proto__ property. However I notice that when I do var f = function() {} f will now have a prototype property in addition to the __proto__ property. It would seem that prototype does not participate in property chaining. What exactly does it do? 回答1: It gets assigned as the prototype of objects created by using that function via the new keyword. So for instance: function Foo() { } Foo.prototype.bar = 47; var obj = new Foo(); alert(obj

Fields are as static fields in Qooxdoo library

╄→尐↘猪︶ㄣ 提交于 2020-01-02 07:27:06
问题 I'd like to use qx-oo (Qooxdoo) as OOP library. But I was confused by strange behaviour of field members. It's looks like that fields are shared between all objects of one class, like static members. For example, this test code qx.Class.define("com.BaseClass", { extend : qx.core.Object, members: { _children: [], getChildrenCount: function(){ return this._children.length; }, addChild: function(child){ this._children.push(child); } } }); var class1 = new com.BaseClass(); var class2 = new com

Will JavaScript ever become a 'proper' class based language? [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-31 04:52:06
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I'm referring to MDN's article on JavaScript's 'future reserved words' (for use in the new strict mode) - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Future_reserved_keywords . All the reserved words indicate that JavaScript will possibly

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

萝らか妹 提交于 2019-12-30 09:00:15
问题 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); //

javascript class inherit from Function class

半腔热情 提交于 2019-12-28 12:05:12
问题 I like that in javascript, I can create a function, and then add further methods and attributes to that function myInstance = function() {return 5} myInstance.attr = 10 I would like to create a class to generate these objects. I assume I have to inherit from the Function base class. In other words, I would like to: var myInstance = new myFunctionClass() var x = myInstance() // x == 5 But I don't know how to create the myFunctionClass. I have tried the following, but it does not work: var

Check if a constructor inherits another in ES6

a 夏天 提交于 2019-12-28 03:09:29
问题 I have a situation where I need to check if a constructor (X) has another constructor (Y) in its prototype chain (or is Y itself). The quickest means to do this might be (new X()) instanceof Y . That isn't an option in this case because the constructors in question may throw if instantiated without valid arguments. The next approach I've considered is this: const doesInherit = (A, B) => { while (A) { if (A === B) return true; A = Object.getPrototypeOf(A); } return false; } That works, but I

inherit prototype methods from other classes without overriding own prototype methods

断了今生、忘了曾经 提交于 2019-12-25 12:51:20
问题 Is there a better way of having a class inherit prototype methods from another class and still be able to define new prototype methods on the class that inherits than this: var ParentConstructor = function(){ }; ParentConstructor.prototype = { test: function () { console.log("Child"); } }; var ChildConstructor = function(){ ParentConstructor.call(this) }; ChildConstructor.prototype = { test2: "child proto" }; var TempConstructor = function(){}; TempConstructor.prototype = ParentConstructor

Javascript Prototypal Inheritance - Truly understanding the concept

感情迁移 提交于 2019-12-25 06:33:27
问题 I was looking at this video explanation(https://www.youtube.com/watch?v=qMO-LTOrJaE) of JS prototypal inheritance. There's something I don't understand. Let me explain var Bear = function(type) { //PARENT this.type; this.hasFur = true; } Bear.prototype.kingdom = 'Animalia'; //ASSIGNING TO PARENT'S PROTOTYPE var Grizzly = function(type) { //CHILD this.species = 'Brown Bear'; } Grizzly.prototype = Object.create(Bear.prototype); //INHERITING FROM PARENT var grizzly1 = new Grizzly('grizzly');

Add formats to native Date parser

穿精又带淫゛_ 提交于 2019-12-24 10:27:09
问题 I was wondering if there was any way to add (and map) formats to the native Date parser in javascript (without using a library). Similar to the defineParsers method that comes with the Mootools extended Date object, if you are familiar with it, but without Mootools. The simplest solution I can think of would be to simply replace the Date prototype parse method with one that wraps the original and rearranges input dates to a valid format first, like this: Date.prototype.parse = (function