object-literal

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

99封情书 提交于 2019-11-27 13:03:22
问题 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() {

JavaScript object literal length === undefined?

心不动则不痛 提交于 2019-11-27 11:33:53
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? 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 count them. Also, your for in loop is prone to bugs due extension of Object.prototype since in will traverse the

JavaScript - Advantages of object literal

耗尽温柔 提交于 2019-11-27 10:08:36
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 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 the module pattern. This still uses object literals, but as the return value from a scoping function: var

How to create an array of object literals in a loop?

北城余情 提交于 2019-11-27 09:56:52
I need to create an array of object literals like this: var myColumnDefs = [ {key:"label", sortable:true, resizeable:true}, {key:"notes", sortable:true,resizeable:true},...... In a loop like this: for (var i = 0; i < oFullResponse.results.length; i++) { console.log(oFullResponse.results[i].label); } The value of key should be results[i].label in each element of the array. var arr = []; var len = oFullResponse.results.length; for (var i = 0; i < len; i++) { arr.push({ key: oFullResponse.results[i].label, sortable: true, resizeable: true }); } RaYell's answer is good - it answers your question.

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

早过忘川 提交于 2019-11-27 08:51:42
问题 This question already has answers here : How to use a variable for a key in a JavaScript object literal? (12 answers) Closed 5 years ago . 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 }; 回答1: No, you can't, you need to feed it after the first initialization : var myKeyName = "bar"; x[myKeyName] = "foo"; 回答2: You need to declare the empty object and build the

reference variable in object literal? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-27 08:25:20
This question already has an answer here: Self-references in object literals / initializers 23 answers 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? Unfortunately, that isn't possible. While an object literal is being constructed, no external reference to that object exists until the entire literal is evaluated. The only way to use this at this stage is to use a

Dynamically Add Variable Name Value Pairs to JSON Object

给你一囗甜甜゛ 提交于 2019-11-27 06:10:16
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 } That's not JSON. It's just Javascript objects, and has nothing at all to do with JSON. You can use brackets to set the properties dynamically. Example: var obj = {}; obj['name'] =

Javascript 'colon' for labeling anonymous functions?

旧街凉风 提交于 2019-11-27 05:07:29
问题 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? 回答1: 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

Arrow Function in Object Literal [duplicate]

给你一囗甜甜゛ 提交于 2019-11-27 05:03:10
This question already has an answer here: Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? 1 answer Methods in ES6 objects: using arrow functions 4 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 works as expected: var functionObject = { name: 'functionObject', printName: function() { console.log(this);

Safely parsing a JSON string with unquoted keys

风流意气都作罢 提交于 2019-11-27 02:39:19
问题 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