prototypal-inheritance

Object.defineProperty in ES5?

末鹿安然 提交于 2019-11-26 23:01:39
I'm seeing posts about a 'new' Object.create that makes enumeration configurable. However, it relies on a Object.defineProperty method. I can't find a cross browser implementation for this method. Are we stuck writing for the old Object.create? I can't write things that won't work in IE6/7. There are several things that you can't emulate from the ECMAScript 5 Object.create method on an ECMAScript 3 environment. As you saw, the properties argument will give you problems since in E3-based implementations there is no way to change the property attributes. The Object.defineProperty method as

What's the difference between this.function and prototype.function?

拜拜、爱过 提交于 2019-11-26 22:53:42
问题 Given simple JS inheritance, what's the practical difference in the base function between these two examples? In other words, when should a person choose to define a function on "this" instead of on the prototype (or the other way around)? For me the second example is easier to digest, but how much more is there to this? function defined on this: //base var _base = function () { this.baseFunction = function () { console.log("Hello from base function"); } }; //inherit from base function _ctor(

Grasping prototypical Inheritance through pseudoclassical instantiation (JavaScript)

Deadly 提交于 2019-11-26 21:06:51
I am attempting to pass a test suite utilizing inheritance through JavaScript. Below is a snippet of the code I have so far: var Infant = function() { this.age = 0; this.color = 'pink'; this.food = 'milk'; }; Infant.prototype.eat = function(){ return this.eat; } var Adolescent = function() { this.age = 5; this.height = 'short'; this.job = 'keep on growing'; }; I would like to inherit the food property from the Infant class and the eat method but my attempts have fallen short. my initial thought was to assign this.Adolescent = Infant.food; but that didn't work. I know I need to set Infant as

Creating new objects from frozen parent objects

≡放荡痞女 提交于 2019-11-26 20:37:43
问题 This example creates an object, freezes it, and then creates a new object from the frozen object. If the second object tries to change the test property, it can't. It remains frozen with the first object's value of 10. //Create an object and freeze it var first = { test: 10 }; Object.freeze(first); //Create a second object from the first one and //try and change the new test property (you can't) var second = Object.create(first); second.test = 20; console.log(second.test); //10 Here are my

Properties of Javascript function objects

萝らか妹 提交于 2019-11-26 17:15:18
I have a JavaScript function object as; var addNum = function(num1, num2) { return num1 + num2; } Now if I try to access addNum.divide() I wanted to understand the prototype chain for the above code. I read that in the above example, addNum would be searched for divide(), followed by Function.prototype and finally Object.prototype. But my question is in the above example, how can addNum would be searched for divide() Does it refer to something like ; var addNum = function(num1, num2) { this.divide = function(){} return num1 + num2; } I could not understand the line where it says addNum would

Extending core types without modifying prototype

瘦欲@ 提交于 2019-11-26 16:25:21
问题 How does one extend core JavaScript types (String, Date, etc.) without modifying their prototypes? For example, suppose I wanted to make a derived string class with some convenience methods: function MyString() { } MyString.prototype = new String(); MyString.prototype.reverse = function() { return this.split('').reverse().join(''); }; var s = new MyString("Foobar"); // Hmm, where do we use the argument? s.reverse(); // Chrome - TypeError: String.prototype.toString is not generic // Firefox -

Good Example of JavaScript's Prototype-Based Inheritance

巧了我就是萌 提交于 2019-11-26 12:34:54
问题 I have been programming with OOP languages for over 10 years but I\'m learning JavaScript now and it\'s the first time I\'ve encountered prototype-based inheritance. I tend to learn fastest by studying good code. What\'s a well-written example of a JavaScript application (or library) that properly uses prototypal inheritance? And can you describe (briefly) how/where prototypal inheritance is used, so I know where to start reading? 回答1: Douglas Crockford has a nice page on JavaScript

Is John Resig's Javascript inheritance snippet deprecated?

邮差的信 提交于 2019-11-26 10:47:05
问题 I\'m looking for a simple way of creating two classes, one inheriting from the other, and the child redefining one of the parent\'s methods, and inside the new method, calling the parent\'s. For example, having a class Animal and Dog , where the Animal class defines a method makeSound() which establishes how to output a sound, which Dog then overrides in its own makeSound() method to make a \"woof\" sound, but while also calling Animal\'s makeSound() to output that woof. I looked at John

How to access object prototype in javascript?

China☆狼群 提交于 2019-11-26 10:18:10
问题 In all the articles it is written that JavaScript is a prototype-based language, meaning that every object has a prototype (or, more precisely, prototype chain). So far, I\'ve tried the following code snippet: var F = function(); F.prototype.member1 = 1; var object1 = new F(); console.log(object1.member1); // prints 1 How can I access the prototype object of object1 ? Is there a browser-neutral way to do that (I mean, not relying on __proto__ property? Seen this link, but maybe there are new

Object.defineProperty in ES5?

二次信任 提交于 2019-11-26 08:30:25
问题 I\'m seeing posts about a \'new\' Object.create that makes enumeration configurable. However, it relies on a Object.defineProperty method. I can\'t find a cross browser implementation for this method. Are we stuck writing for the old Object.create? I can\'t write things that won\'t work in IE6/7. 回答1: There are several things that you can't emulate from the ECMAScript 5 Object.create method on an ECMAScript 3 environment. As you saw, the properties argument will give you problems since in E3