object-literal

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

人盡茶涼 提交于 2019-11-26 19:53:41
问题 This question already has an answer here: What is the difference between object keys with quotes and without quotes? 5 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:

Object literal vs constructor+prototype

可紊 提交于 2019-11-26 18:56:52
问题 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

JavaScript object literal length === undefined?

♀尐吖头ヾ 提交于 2019-11-26 18:03:20
问题 I am working on this animation function but I have a problem. I can't seem to perform what should be an easy task, I can not get the length of an object. If you check out that jsFiddle you can see that I am running alert(properties.length); and it is returning undefined . Can anyone see why this might be? 回答1: JavaScript object simply do not have a length property, only Arrays do. If you want to know the number of properties that are defined on a object, you have to iterate over them and

JavaScript - Advantages of object literal

别来无恙 提交于 2019-11-26 17:55:59
问题 I've read that rather than simply writing a bunch of functions, I should use object literal. Can someone explain what the advantages of object literal are with examples, because I don't understand thus far. Thanks 回答1: As Russ Cam said, you avoid polluting the global namespace, which is very important in these days of combining scripts from multiple locations (TinyMCE, etc.). As Alex Sexton said, it makes for good code organisation as well. If you're using this technique, I'd suggest using

Template String As Object Property Name

社会主义新天地 提交于 2019-11-26 17:35:46
Why does JavaScript not allow a template string as an object property key? For example, when I input: foo = {`bar`: 'baz'} into the NodeJS REPL, it throws a SyntaxError with "Unexpected template string" with a long stack trace. Property values are fine, however, which is not as unexpected. Similar errors happen in the browser, for example, Firebug throws a SyntaxError with "invalid property id". Template strings are allowed in "computed property names". For instance, this compiles perfectly fine in all browsers that support the syntax: var foo = { [`bar` + 1]: `baz` }; and creates the object {

reference variable in object literal? [duplicate]

孤人 提交于 2019-11-26 14:08:12
问题 This question already has answers here : Self-references in object literals / initializers (23 answers) Closed 4 years ago . say I have myfunc({ var1: 1, var2: 2, }) if i want to have a value that makes use of the current unnamed object, how would I do this? eg if I wanted myfunc({ var1: 1, var2: 2, var3: this.var1 + this.var2 }) obviously this does not work. What would the correct syntax be? 回答1: Unfortunately, that isn't possible. While an object literal is being constructed, no external

Dynamically Add Variable Name Value Pairs to JSON Object

余生长醉 提交于 2019-11-26 12:53:20
问题 I have a json object full of ips like var ips = {} I then add ip objects to this object like so ips[ipID] = {} I then need to add dynamic/variable name value pairs to each ip so I am using code like this var name; var value; var temp = {}; tmp[name] = value My question is, how can I add these name value pairs/ tmp to my ipID objects so that my outcome turns out like ipID = { name : value, anotherName : anotherValue } 回答1: That's not JSON. It's just Javascript objects, and has nothing at all

Arrow Function in Object Literal [duplicate]

假装没事ソ 提交于 2019-11-26 11:14:30
问题 This question already has an answer here: Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? 1 answer Methods in ES6 objects: using arrow functions 3 answers I\'m trying to figure out why an arrow function in an object literal is called with window as this . Can someone give me some insight? var arrowObject = { name: \'arrowObject\', printName: () => { console.log(this); } }; // Prints: Window {external: Object, chrome: Object ...} arrowObject.printName(); And an object that

Use a concatenated (dynamic) string as JavaScript object key? [duplicate]

試著忘記壹切 提交于 2019-11-26 06:38:50
问题 This question already has answers here : Is it possible to add dynamically named properties to JavaScript object? (17 answers) Closed 2 years ago . var test = \"test123\" var test123 ={ \"key\" + test: 123 } This code doesn\'t work. What is wrong with \"key\" + test ? 回答1: Because "key" + test is an expression and not an identifier nor a string literal nor a number literal, which are the only things that are allowed as the key in an object literal. You have to use the [] notation after

create object using variables for property name [duplicate]

岁酱吖の 提交于 2019-11-26 04:39:24
问题 This question already has an answer here: How to use a variable for a key in a JavaScript object literal? 12 answers Is it at all possible to use variable names in object literal properties for object creation? Example function createJSON (propertyName){ return { propertyName : \"Value\"}; } var myObject = createJSON(\"myProperty\"); console.log(myObject.propertyName); // Prints \"value\" console.log(myObject.myProperty); // This property does not exist 回答1: If you want to use a variable for