Questions tagged [object-literal]

JavaScript Object Literal An object literal is a comma separated list of name value pairs wrapped in curly braces. In JavaScript an object literal is defined as follows: var myObject = { sProp: 'some string value', numProp: 2, bProp: false };

Literal means fixed value in source code. In ECMAScript scripting language its possible to represent object as literal. e.g.

  var newobj = {
  var1: true,
  var2: "very interesting",
  method1: function () {
    alert(this.var1)
  },
  method2: function () {
    alert(this.var2)
  }
};
newobj.method1();
newobj.method2(); 

Attribution : http://en.wikipedia.org/wiki/Literal_%28computer_programming%29

Objects in JavaScript

570 questions
-1
votes
2 answers

js converting string with [*] into variable

I've got basic variable that I'm getting from my api: nurseListSpeciality.avatar = "srcs[14]" I want to change srcs[14] into array name, that my object should be like: nurseListSpeciality:[{avatar: srcs[14]}] The main problem is that I don't want…
gileneusz
  • 1,435
  • 8
  • 30
  • 51
-1
votes
2 answers

I can't bind event to remove field in jQuery

I make this script in jQuery to add and remove fields, first I separate the functions in object literal way, and I can add fields by cloning the div. But I can't bind the remove button to cloned element.
-1
votes
2 answers

Why does 'duplicates' property keeps returning 3 instead of 2?

The task I am undertaking is as follows: The function should be called removeDuplicates and should return an object literal containing a 'uniques' property, which should be the sorted input string but without any duplicates or special…
danoseun
  • 69
  • 2
  • 9
-1
votes
1 answer

Why doesn't the instance variable take the new value

Here is a code example: var testObject = { val1: 1, testing: function( ) { val1 = 2; alert( val1 ); } }; how come when alert prints val1, it's says undefined?
dave
  • 14,991
  • 26
  • 76
  • 110
-1
votes
1 answer

Javascript class constructor with parameter used as key from object literal

Title might be a little confusing, but I have following problem: var propertyList = { type1: {property1: 1}, type2: {property1: 2} } class Test { constructor(typ){ this.property1 = propertyList.typ.property1; } } var a =…
Furman
  • 2,017
  • 3
  • 25
  • 43
-1
votes
2 answers

Why does jQuery code not work after organize with object literal?

So I've been trying to organize my jQuery code (which currently has everything in $(document).ready()) by implementing an object literal on the #Menu. But now the event delegation that used to work is no longer working. HTML
-1
votes
2 answers

Looping through object literal with nested functions

I'm trying to loop through an object literal and select just one of the keys and return it's value, which will be a function. My problem is, that I can't get the loop to return just one value - it always returns all of the values (functions). I…
MortiestMorty
  • 635
  • 1
  • 5
  • 13
-1
votes
1 answer

Creating a dictionary in PHP

I have the data in the variable $appLangs as : [{"id":"1","site":"7","elemId":"navbar_title","en":"Home","de":"Zuhause","fr":"accueil"},{"id":"2","site":"7","elemId":"home_untilNextTime","en":"Until Next Time","de":"Bis zum n\u00e4chsten…
Surily
  • 121
  • 2
  • 10
-1
votes
1 answer

Javascript - Inheritence and reference

In javascript, other than prototypal inheritence using prototypes, is there any other way of inheritence. Is the use of object literal only for creating a singleton class. What is a reference when it comes in javascript. What are the uses of closure…
-1
votes
4 answers

Passing unknown amounts of variables using through a string string and eval and multiple functions and all sorts

In short, I want to use an object literal to allow me to pass a unknown amount of variables in any order to a function. Whilst this is not big deal in theory, in my code, this object literal is passed to a second function called on_change. on_change…
thecoshman
  • 8,394
  • 8
  • 55
  • 77
-1
votes
1 answer

Object literal property and method location

Do all methods have to be stated after all properties in JavaScript when defining objects in object literal form? I have tried voiding this idea and it seems that all properties stated afterwards are not included.
-1
votes
2 answers

how to write an array literal notation in JS?

how to write an array literal notation in JS? var myArray = new Array(); myArray.prop = 'test'; i've already tried this, but it logs undefined value. var myArray = [prop='test']; console.log(myArray.prop);
user2271066
  • 61
  • 1
  • 2
-1
votes
2 answers

Does JavaScript hoist if statements when object literals are involved?

var foo = {}; document.body.innerHTML = console.log = location.hash = 'Hi ' + '
' + foo.bar + '
' + foo.baz; setTimeout(function() { foo.baz = foo["bar"] = []; foo.bar.push(new Date); foo.baz.push(new Date);…
-1
votes
2 answers

Should I reference an object variable's name from within the object's implementation?

Is there any difference between (setting a property value) with : var obj = { getData: function () { this.age = 34 //notice this } } obj.getData(); alert(obj.age) //34 vs var obj = { getData: function () { …
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
-2
votes
3 answers

JavaScript Object Literal - how to replace `this` keyword for another thing

Is there any way to avoid the constant use of the this keyword to access the properties and methods of the current object? I tried something like in Example 2, but it doesn't work at all. Example 1: