FAQ Database Discussion Community


Call parents implementation in Object.defineProperty

javascript,inheritance,prototypal-inheritance,defineproperty
I use javascript prototypal inheritance where A "inherits" B. B uses defineProperty to define a setter for property prop. In A I want to override this behaviour: Function.prototype.inherits = function (parent) { this.prototype = Object.create(parent.prototype); this.prototype.constructor = parent; }; // -------------------------------------------- var B = function() { this.myProp = 0; };...

node.js object inheritance overwriting object methods

node.js,prototypal-inheritance
I am trying to port a Java application for use in node.js, and am running into an issue with Object inheritance. I have a base object HVal, and 2 subclasses, HBin and HBool. When I try to use both HBin and HBool, the 1st object that is loaded is overridden...

Ruby Method Lookup (comparison with JavaScript)

javascript,ruby,inheritance,prototypal-inheritance
I want to better understand how objects in Ruby have access methods defined in classes and modules. Specifically, I want to compare and contrast it with JavaScript (which I'm more familiar with). In JavaScript, objects look up methods on the object itself and if it can't find it there, it'll...

Sensible approach to callbacks on object prototype methods in javascript/jquery?

javascript,jquery,ajax,closures,prototypal-inheritance
Is what I've done below a sensible approach to allow callbacks to run on functions defined in an object's prototype, such that the scope is correct? I've been wrestling with the correct way to set the value of this when an object's prototype method is the one to run in...

why is functionName() == functionName.prototype.constructor()

javascript,function,prototypal-inheritance
I noticed if I have the following: var functionName = function(){console.log("this is a function!")} calling functionName is equivalent to functionName.prototype.constructor because they both hold the function definition above. I guess I would just like an explanation on how function are used as objects in JavaScript and how that relates to...

How can I check instanceof without the proto chain in javascript?

javascript,prototype,prototypal-inheritance
How can I check instanceof without the proto chain in javascript? var EventEmitter = require('events').EventEmitter; var Foo = function(){ }; Foo.prototype = EventEmitter.prototype; var Bar = function(){ }; Bar.prototype = EventEmitter.prototype; var f = new Foo(); var b = new Bar(); f instanceof Foo; //returns true b instanceof Bar; //returns...

Difference between Object.create(prototype) and Object(prototype)

javascript,oop,object,prototypal-inheritance
A quick but hard-to-google question: var child = Object.create(parent.prototype); var child = Object(parent.prototype); Are they identical? edit: My question was raised by this two examples of inheritPrototype functions used to implement the Parasitic Combination Inheritance Pattern. function inheritPrototype(childObject, parentObject) { var copyOfParent = Object.create(parentObject.prototype); copyOfParent.constructor = childObject; childObject.prototype = copyOfParent;...

get dynamic property defined in prototype during JSON.stringify

javascript,prototype,prototypal-inheritance
I've defined an enumerable property in the prototype object and would like it to appear when I convert a prototyped object to JSON. My first idea was to set it in toJSON but because I don't really want to keep it in the object afterwards I'll have to more or...

jQuery Plugin Development how to keep variables and methods private

javascript,jquery-plugins,prototypal-inheritance,private-members
For exercise purposes I am creating a jQuery Plugin, a simple Image Slider. I use a Pattern from Boilerplate - jQuery Plugins. During the initializing process everything works as expected, every Instance gets the right values (widths and heights, also event binding) needed for setup. The trouble starts when I...

javascript native prototype: extend, add and overwrite methods?

javascript,prototype,prototypal-inheritance
How can I extend a prototype and add new methods into it? For instance I want to extend Shape (superclass) into a subclass - Rectangle. I am extending it because I want to use the methods in Shape, but add more methods (and overwrite some Shape's methods as well) in...

How to inherit from base provider (not the provider factory)?

angularjs,prototypal-inheritance,angularjs-provider
Say I have this base provider: angular.module('app').provider('BaseClient', function () { this.setSomething = function (something) { // Store `something` somewhere return this; }; }); And now these 2 other sub-providers: angular.module('app').provider('ClientA', function () { this.$get = function () { return { foo: function () { console.log('FOO', /* Read `something`, needs to...

Javascript class inheritance. Subclass changes main class's properties?

javascript,inheritance,prototype,prototypal-inheritance
var Main = function(){}; Main.prototype = { 'times' : 0, 'request': function(){}, ... }; var SubA = function(){}; SubA.prototype = new Main() // same with Object.create(Main.prototype); SubA.prototype.constructor = SubA; var SubB = function(){}; SubB.prototype = new Main() // same with Object.create(Main.prototype); SubB.prototype.constructor = SubB; Now: var sub_a = new SubA();...

What's the difference between these 2 implementations of prototypal inheritance?

javascript,inheritance,prototype,prototypal-inheritance
What's the difference between these 2 implementations of prototypal inheritance, and considering that we're working with 2 different "prototypes" (the prototype property that's only on functions, and the internal prototype), and how do these implementations differ in their prototype chain lookup? Also, does the first implementation (using the prototype property)...

Object prototypes and Inheritance

javascript,prototypal-inheritance
// the original Animal class and sayName method function Animal(name, numLegs) { this.name = name; this.numLegs = numLegs; } Animal.prototype.sayName = function() { console.log("Hi my name is " + this.name); }; // define a Penguin class function Penguin() { this.name = "penguin"; this.numLegs = 2; } // set its prototype...

Javascript Inheritance Overriding

javascript,inheritance,design-patterns,prototypal-inheritance
There's a lot of confusing literature on the net about Javascript overriding. But, I haven't found one any that does what I need. I'm trying to enable inheritance and method override, but in a particular way. Imagine: var A = function(amount) { this.amount = amount; var _this = this; this.method1...

javascript chainable nested subclass

javascript,prototype,prototypal-inheritance
I'm effectively trying to build a chainable object that contains sub objects or other functions. It should be used like this: val().rules.phone("someValue"); So far: var val = function(){ var validator = function(){ this.fields = []; return this; } validator.prototype = { addField: function(fieldName){ this.fields.push(fieldName); return this; }, rules: { phone:...

Why does the __proto__ object evaluate to “Object” in the debugger?

javascript,debugging,prototype,google-chrome-devtools,prototypal-inheritance
I'm reading Kyle Simpson's "YDKJS: this & Object Prototypes", and looking at his example of behavior delegation. Here's the code below: Foo = { init: function(who) { this.me = who; }, identify: function() { return "I am " + this.me; } }; Bar = Object.create( Foo ); Bar.speak = function()...

How can I change the scope/context of a call to a javascript property?

javascript,prototypal-inheritance
I think it is easiest to explain my question with a simple example. Imagine there exists a simple Javascript class hierarchy involving the classes Shape and Square where Square 'inherits' from Shape. Also, in this scenario, Shape has a property named 'colour' (an actual property created using Object.defineProperty on the...