object-literal

in javascript is it possible to construct an object literal with expressions evaluating to strings for property names? [duplicate]

末鹿安然 提交于 2019-11-28 14:41:54
This question already has an answer here: How to use a variable for a key in a JavaScript object literal? 12 answers i.e. is it possible to do this: var fruit = "banana"; var x = { "app" + "le" : 5, // "apple" : 5 function(){return "orange"} : 8, // "orange" : 8 "" + fruit : 3 // "banana" : 3 }; No, you can't, you need to feed it after the first initialization : var myKeyName = "bar"; x[myKeyName] = "foo"; You need to declare the empty object and build the strings after. An object literal expects valid strings for its names If you don't run the function for 'orange' as well as define it, the

Safely parsing a JSON string with unquoted keys

霸气de小男生 提交于 2019-11-28 09:05:49
json2.js is strict requiring all object keys be double-quoted. However, in Javascript syntax {"foo":"bar"} is equivalent to {foo:"bar"} . I have a textarea that accepts JSON input from the user and would like to "ease" the restriction on double quoting the keys. I've looked at how json2.js validates a JSON string in four stages before it evals it. I was able to add a 5th stage to allow unquoted keys and would like to know if there are any security implications to this logic. var data = '{name:"hello", age:"23"}'; // Make sure the incoming data is actual JSON // Logic borrowed from http://json

Javascript 'colon' for labeling anonymous functions?

大兔子大兔子 提交于 2019-11-28 03:27:04
What does this code refer too? queryString: function() { //some code } I tested it in the WebConsole (Firefox) but it wouldn't execute, so I'm thinking that it isn't equivalent to function queryString() {} . So what is it exactly? You are missing some code there, but I assume its part of an object declaration like this: var obj = { queryString: function() { //some code } }; obj.queryString(); It assigns a function as a property of an object literal. It would be equivalent to this: var obj = {}; obj.queryString = function() { ... }; obj.queryString(); In general, the object literal syntax looks

Nested Object Literal Access Parent

核能气质少年 提交于 2019-11-27 19:38:31
问题 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. 回答1: This actually can't be done (without hacking using call or apply the way @RichardMacarthy showed) because you are creating a new

Why are some object-literal properties quoted and others not? [duplicate]

空扰寡人 提交于 2019-11-27 19:21:23
This question already has an answer here: What is the difference between object keys with quotes and without quotes? 6 answers I see this all the time: object literals declared such that some keys are surrounded with quotes and others are not. An example from jQuery 1.4.2: jQuery.props = { "for": "htmlFor", "class": "className", readonly: "readOnly", maxlength: "maxLength", cellspacing: "cellSpacing", rowspan: "rowSpan", colspan: "colSpan", tabindex: "tabIndex", usemap: "useMap", frameborder: "frameBorder" }; What is the significance of wrapping the first two property keys ( for and class )

Object literal vs constructor+prototype

混江龙づ霸主 提交于 2019-11-27 17:20:38
Object literal =name value pairs wrapped in curly braces. Constructor =a function used to create multiple instance using keyword new. Prototype =for extension of a literal. This is what I have understood till now.But the more I research,More I get confused about what is the significance of each one of them. I have used constructor,protoypes and literals in my code a few times.But everytime I use them,I feel like I am still not aware of the full potential of it .I want to go one step ahead of being a begineer now.I hope Folks at stackoverflow help me achieve it Which is the best preferred way

Dynamic object literal in javascript?

六眼飞鱼酱① 提交于 2019-11-27 16:22:26
问题 Is it possible to creat an object literal on the fly? Like this: var arr = [ 'one', 'two', 'three' ]; var literal = {}; for(var i=0;i<arr.length;i++) { // some literal push method here! /* literal = { one : "", two : "", three : "" } */ } Thus I want the result to be like this: literal = { one : "", two : "", three : "" } 回答1: for ( var i = 0, l = arr.length; i < l; ++i ) { literal[arr[i]] = "something"; } I also took the liberty of optimising your loop :) 回答2: Use this in your loop: literal

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

末鹿安然 提交于 2019-11-27 14:32:51
问题 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!") } } 回答1: 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

How do I properly run the VJET development tools for NodeJS on Eclipse?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 14:11:52
问题 The default javascript editor for Eclipse has very poor outlining and code completion . As a result of this, for any modern javascript application like ExtJS or NodeJS where you need to write a lot of object literal statements, Eclipse becomes pretty useless. And it is impossible to ask how to do this properly. Now I found that Ebay Open Source seems to have tools that are specifically designed to replace this flaw in the standard editors, both for javascript in general as for NodeJS

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

不羁岁月 提交于 2019-11-27 13:15:25
问题 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