object-create

Javascript Object.create not working in Firefox

自古美人都是妖i 提交于 2019-11-27 15:19:15
I always get the following exception in Firefox (3.6.14): TypeError: Object.create is not a function It is quite confusing because I am pretty sure it is a function and the code works as intended on Chrome. The lines of code responsible for this behavior are the following: Object.create( Hand ).init( cardArr ); Object.create( Card ).init( value, suit ); It is from a poker library gaga.js if someone wants to see all the code: https://github.com/SlexAxton/gaga.js Maybe someone knows how to get it working in Firefox? Object.create() is a new feature of EMCAScript5. Sadly it is not widely

JavaScript Object.create — inheriting nested properties

我的未来我决定 提交于 2019-11-27 12:41:35
问题 I've come across a peculiarity with Douglas Crockfords Object.create method which I'm hoping someone might be able to explain: If I create an object - say 'person' - using object literal notation then use Object.create to create a new object - say 'anotherPerson' - which inherits the methods and properties from the initial 'person' object. If I then change the name values of the second object - 'anotherPerson' - it also changes the name value of the initial 'person' object. This only happens

JavaScript inheritance with Object.create()?

这一生的挚爱 提交于 2019-11-27 01:03:07
问题 How do I inherit with the Object.create()? I tried these, but none are working: var B = function() {}; var A = function() {}; A = Object.create(B); A.prototype.C = function() {}; and var B = function() {}; var A = function() {}; A.prototype.C = function() {}; A = Object.create(B); and var B = function() {}; A = Object.create(B); var A = function() {}; A.prototype.C = function() {}; Nothing worked. How am I supposed to use this new Object.create()-function? 回答1: Object.create() is used to

Javascript Arrays created with Object.create - not real Arrays?

99封情书 提交于 2019-11-26 23:10:08
问题 It looks like Arrays created with Object.create walk like Arrays and quack like Arrays, but are still not real arrays. At least with v8 / node.js. > a = [] [] > b = Object.create(Array.prototype) {} > a.constructor [Function: Array] > b.constructor [Function: Array] > a.__proto__ [] > b.__proto__ [] > a instanceof Array true > b instanceof Array true > Object.prototype.toString.call(a) '[object Array]' > Object.prototype.toString.call(b) '[object Object]' Can some Javascript guru explain why

Javascript Object.create not working in Firefox

爱⌒轻易说出口 提交于 2019-11-26 22:25:08
问题 I always get the following exception in Firefox (3.6.14): TypeError: Object.create is not a function It is quite confusing because I am pretty sure it is a function and the code works as intended on Chrome. The lines of code responsible for this behavior are the following: Object.create( Hand ).init( cardArr ); Object.create( Card ).init( value, suit ); It is from a poker library gaga.js if someone wants to see all the code: https://github.com/SlexAxton/gaga.js Maybe someone knows how to get

JavaScript inheritance: Object.create vs new

白昼怎懂夜的黑 提交于 2019-11-26 13:57:18
In JavaScript what is the difference between these two examples: Prerequisite: function SomeBaseClass(){ } SomeBaseClass.prototype = { doThis : function(){ }, doThat : function(){ } } Inheritance example A using Object.create: function MyClass(){ } MyClass.prototype = Object.create(SomeBaseClass.prototype); Inheritance example B using the new keyword function MyClass(){ } MyClass.prototype = new SomeBaseClass(); Both examples seem to do the same thing. When would you chose one over the other? An additional question: Consider code in below link (line 15), where a reference to the the function's

Understanding Crockford's Object.create shim

主宰稳场 提交于 2019-11-26 09:13:28
问题 I\'ve been reading up on the Crockford shim for preventing the overwriting of prototypes, and understand that it\'s not the end-all/be-all solution at times. I also understand that ES5 Shim may be a viable alternative to this. I also read this post which provides a more robust, secure alternative. Still, I\'d like to know what his Object.create shim is \"saying\" and then \"doing.\" Can someone please tell me if my explanation comments are right? if (typeof Object.create === \'undefined\') {

JavaScript inheritance: Object.create vs new

自古美人都是妖i 提交于 2019-11-26 03:01:43
问题 In JavaScript what is the difference between these two examples: Prerequisite: function SomeBaseClass(){ } SomeBaseClass.prototype = { doThis : function(){ }, doThat : function(){ } } Inheritance example A using Object.create: function MyClass(){ } MyClass.prototype = Object.create(SomeBaseClass.prototype); Inheritance example B using the new keyword function MyClass(){ } MyClass.prototype = new SomeBaseClass(); Both examples seem to do the same thing. When would you chose one over the other?

Understanding the difference between Object.create() and new SomeFunction()

北战南征 提交于 2019-11-26 01:22:12
问题 I recently stumbled upon the Object.create() method in JavaScript, and am trying to deduce how it is different from creating a new instance of an object with new SomeFunction() , and when you would want to use one over the other. Consider the following example: var test = { val: 1, func: function() { return this.val; } }; var testA = Object.create(test); testA.val = 2; console.log(test.func()); // 1 console.log(testA.func()); // 2 console.log(\'other test\'); var otherTest = function() { this

Using “Object.create” instead of “new”

烂漫一生 提交于 2019-11-25 23:09:11
问题 Javascript 1.9.3 / ECMAScript 5 introduces Object.create , which Douglas Crockford amongst others has been advocating for a long time. How do I replace new in the code below with Object.create ? var UserA = function(nameParam) { this.id = MY_GLOBAL.nextId(); this.name = nameParam; } UserA.prototype.sayHello = function() { console.log(\'Hello \'+ this.name); } var bob = new UserA(\'bob\'); bob.sayHello(); (Assume MY_GLOBAL.nextId exists). The best I can come up with is: var userB = { init: