object-literal

Why can't I save an object's methods as properties of another object literal

雨燕双飞 提交于 2019-12-11 15:25:35
问题 The code below is used to note some methods to run in particular circumstances so they can be called using a simpler syntax. var callbacks = {alter: SPZ.sequenceEditor.saveAndLoadPuzzle, copy: SPZ.sequenceEditor.saveAsCopyAndLoadPuzzle, justSave:SPZ.sequenceEditor.saveAndLoadPuzzle}; But the code keeps returning an empty object. I've checked with console.log that the methods are defined. I've also tried changing the names, invoking an empty object and then adding the properties as eg

How far can an object literal be nested? [closed]

元气小坏坏 提交于 2019-12-11 11:41:57
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I have found that it is possible to nest another property within an object literal property. Here is an example of this: var rectangle = { upperLeft : {

Self-references in object literals / initializers

穿精又带淫゛_ 提交于 2019-12-11 06:35:24
问题 Is there any way to get something like the following to work in JavaScript? var foo = { a: 5, b: 6, c: this.a + this.b // Doesn't work }; In the current form, this code obviously throws a reference error since this doesn't refer to foo . But is there any way to have values in an object literal's properties depend on other properties declared earlier? 回答1: Well, the only thing that I can tell you about are getters: var foo = { a: 5, b: 6, get c() { return this.a + this.b; } } console.log(foo.c

Using object literal for prototype classes

北战南征 提交于 2019-12-11 04:49:23
问题 I'm writing some JavaScript classes ( old school and NOT using ES2015/ES6 and I don't want to use Babel or other transpilers ) and I have one that inherits from the other, overriding one of the parent methods. So I have my initial App.Hello class: var App = {}; App.Hello = function(args) { this.name = args.name; } App.Hello.prototype = { constructor: App.Hello, sayHello: function() { console.log('Hello, ' + this.name); }, sayGoodbye: function() { console.log('Goodbye, ', this.name); } } An

Javascript: How to return or parse an object literal with eval?

浪尽此生 提交于 2019-12-11 04:46:39
问题 I have a little library that takes strings and constructs objects out of them. For example '-key val' creates {"key": "val"} . However I'm trying to extend the syntax of the input string to take simple object literals too, such as '-key "{key: 'val'}"' which should yield {"key" : {"key" : "val"}} however the result is only {"key" : "val"} . Why does eval only return "val" and not the entire object? And is there a safer alternative then my solution? // my code before the fix var arg = '{key:

Get link value from object literal, onchange--Javascript/HTML select

这一生的挚爱 提交于 2019-12-11 00:37:09
问题 I know how to operate this menu with a switch case routine, but I want to change my switch case to an object literal. Part A knows how to get the onchange value and open a window. Part B knows how to find the value in a name:value pair in an object, but only if it's given a hard-coded name to match a value to. I have to combine Part A and Part B's capabilities. Since Part A and Part B both work independently of each other, it should be a simple matter to combine them, but I've been at it four

How to use properties of an object literal without being inside a function in javascript

我与影子孤独终老i 提交于 2019-12-10 17:09:47
问题 I created an object literal in JS, but wanna to access a property without being in a function. Whenever I try, I don't get any results or it returns null . JS: var settings = { prev: '#prev', next: '#next', slides: '#slides', crslContainer: '#carousel', //I'm trying to access the 'slides' by using 'this.slides' inDent: $(this.slides).find('li').outerWidth(), fx: 'rotate', myFunction: function () { console.log(settings.inDent); //This shows null in the console, why?? } } In the code above I'm

Issues creating object literal using anonymous types in c#

人走茶凉 提交于 2019-12-10 10:26:47
问题 I'm trying to build the c# approximation of a JavaScript object literal to be passed to a view model in asp.net MVC: var obj = new dynamic[]{ new { name: "Id", index: "Id", width: 40, align: "left" }, new { name: "Votes", index: "Votes", width: 40, align: "left" }, new { name: "Title", index: "Title", width: 200, align: "left"} }; The compiler is throwing: "An anonymous type cannot have multiple properties with the same name" Stab in the dark I'm guessing it can't distinguish between the

How to access an outer member from a nested object literal?

☆樱花仙子☆ 提交于 2019-12-09 16:51:51
问题 In the following code, can the x member be accessed from the nested object literal? var outer = { x : 0, inner: { a : x + 1, // 'x' is undefined. b : outer.x + 1, // 'outer' is undefined. c : this.x + 1 // This doesn't produce an error, } // but outer.inner.c is NaN. } 回答1: In the way you put it - no. You need two stages construction, this will work: var outer = { x : 0 }; // outer is constructed at this point. outer.inner = { b : outer.x + 1 // 'outer' is defined here. }; 回答2: Not in the

Reference nested 'sibling'-property in object literal

大兔子大兔子 提交于 2019-12-08 17:28:38
问题 I want to reference a nested property in an object literal from within another property in that same object literal. Consider the following contrived example: var obj = { product1: { price: 80, price_was: 100, discount: function(){ return 100 - (100 * (price/price_was)); //I don't want to use: //100 - (100 * (this.product1.price/this.product1.price_was)) //because the name of the parent ('product1' in this case) isn't known //a-priori. } } } The above is obviously incorrect, but how to get to