object-literal

Change value of object property inside javascript object affect other object

↘锁芯ラ 提交于 2019-12-02 02:59:41
问题 I would like to change a property of an object, inside an object. But, when I did that, other object property that created using the same prototype also changed. The code is as follows: var a = { x: { y: 'foo' } } var b = Object.create(a) var c = Object.create(a) console.log(a.x.y) // 'foo' console.log(b.x.y) // 'foo' console.log(c.x.y) // 'foo' b.x.y = 'bar' var d = Object.create(a) console.log(a.x.y) // 'bar' console.log(b.x.y) // 'bar' console.log(c.x.y) // 'bar' console.log(d.x.y) // 'bar

JavaScript - passing an object literal as second arg to Object.create()

徘徊边缘 提交于 2019-11-30 21:59:36
Referring to the JavaScript code snippet below, questions: Why does the object literal {item: {value: "foobar"}} behave differently when assigned to a variable (like in line 1) vs. when passed as an argument to Object.create() (like in line 5)? What is the difference between line 5 and line 8 - i.e. why is line 5 the correct way to pass the second argument to Object.create() and not line 8 (to override the item property in delegate)? Code Snippet: 1 var obj = {item: {value: "foobar"}}; 2 console.log(obj.item); // [object Object] 3 console.log(obj.item.value); // foobar 4 var delegate = {item:

PHP object literal

允我心安 提交于 2019-11-30 11:43:13
问题 In PHP, I can specify array literals quite easily: array( array("name" => "John", "hobby" => "hiking"), array("name" => "Jane", "hobby" => "dancing"), ... ) But what if I want array of objects? How can I specify object literal in PHP? I.e. in javascript it would be: [ {name: "John", hobby: "hiking"}, {name: "Jane", hobby: "dancing"} ] 回答1: As BoltClock mentioned there is no object literal in PHP however you can do this by simply type casting the arrays to objects: $testArray = array( (object

How can I differentiate between an object literal other Javascript objects?

独自空忆成欢 提交于 2019-11-30 03:01:40
Update : I'm rephrasing this question, because the important point to me is identifying the object literal: How can I tell the difference between an object literal and any other Javascript object (e.g. a DOM node, a Date object, etc.)? How can I write this function: function f(x) { if (typeof x === 'object literal') console.log('Object literal!'); else console.log('Something else!'); } So that it only prints Object literal! as a result of the first call below: f({name: 'Tom'}); f(function() {}); f(new String('howdy')); f('hello'); f(document); Original Question I'm writing a Javascript

PHP object literal

半腔热情 提交于 2019-11-30 01:10:27
In PHP, I can specify array literals quite easily: array( array("name" => "John", "hobby" => "hiking"), array("name" => "Jane", "hobby" => "dancing"), ... ) But what if I want array of objects? How can I specify object literal in PHP? I.e. in javascript it would be: [ {name: "John", hobby: "hiking"}, {name: "Jane", hobby: "dancing"} ] As BoltClock mentioned there is no object literal in PHP however you can do this by simply type casting the arrays to objects: $testArray = array( (object)array("name" => "John", "hobby" => "hiking"), (object)array("name" => "Jane", "hobby" => "dancing") ); echo

Can one set multiple properties inside an object literal to the same value?

旧城冷巷雨未停 提交于 2019-11-28 23:11:19
For example, can I do this?: { a: b: c: d: 1, e: 2, geh: function() { alert("Hi!") } } EDIT: Is there some way I can avoid doing this?: { a: 1, b: 1, c: 1, d: 1, e: 2, geh: function() { alert("Hi!") } } You could set a line of equality between various properties: var foo = {}; foo.a = foo.b = foo.c = "Hello"; Or you could just create a method that does the mass-assignment for you: var foo = { setValue: function( props, value ) { while ( props.length ) this[ props.pop() ] = value; } } foo.setValue( [ "a", "b", "c" ] , "Foo" ); An update to this (in terms of the latest JavaScript abilities)

How to add private variable to this Javascript object literal snippet?

可紊 提交于 2019-11-28 20:41:53
Found this at MDC but how if I'd wanted to add a private variable to the var dataset = { tables:{ customers:{ cols:[ /*here*/ ], rows:[ /*here*/ ] }, orders:{ cols:[ /*here*/ ], rows:[ /*here*/ ] } }, relations:{ 0:{ parent:'customers', child:'orders', keyparent:'custid', keychild:'orderid', onetomany:true } } } The way I understand OOP in Javascript, I'd have access to dataset.tables.customers.cols[0] if such an item exists. But if I wanted to place a private variable into customers, what would that look like? Adding var index = 0; results in a runtime error. Peter Bailey You can't have

Call functions from function inside an object (object literal)

杀马特。学长 韩版系。学妹 提交于 2019-11-28 18:12:53
I'm learning to use object literals in JS, and I'm trying to get a function inside an object to run by calling it through another function in the same object. Why isn't the function "run" running when calling it from the function "init"? var runApp = { init: function(){ this.run() }, run: function() { alert("It's running!"); } }; That code is only a declaration . You need to actually call the function: runApp.init(); Demo: http://jsfiddle.net/mattball/s6MJ5/ There is nothing magical about the init property of an object, which you happen to have assigned a function to. So if you don't call it,

Javascript data structure for fast lookup and ordered looping?

妖精的绣舞 提交于 2019-11-28 16:53:52
is there a data structure or a pattern in Javascript that can be used for both fast lookup (by key, as with associative arrays) and for ordered looping? Right, now I am using object literals to store my data but I just disovered that Chrome does not maintain the order when looping over the property names. Is there a common way to solve this in Javascript? Thanks for any hints. Create a data structure yourselves. Store the ordering in an array that is internal to the structure. Store the objects mapped by a key in a regular object. Let's call it OrderedMap which will have a map, an array, and

Nested Object Literal Access Parent

三世轮回 提交于 2019-11-28 14:42:14
I'm trying to access a parent within an object literal graph and I am not sure the correct way to accomplish this. Here is some pseudo code. function MealPlan() { this.sets = [] } MealPlan.prototype = { protein: { getTotalSets: function() { return this.sets.length; } } }; I am trying to get a hold of the sets property from within the getTotalSets function. This actually can't be done (without hacking using call or apply the way @RichardMacarthy showed) because you are creating a new context by creating a new object context (e.g. protein ) on the prototype. The prototype is used to add methods