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
4
votes
2 answers

Referring to previously defined properties within an object literal

When using object constructors, properties can be directly assigned to the value of previously defined properties: var foo = new (function() { this.bar = 5; this.baz = this.bar; })(); alert(foo.baz) // 5 I would like to refer to a previously…
Chiraag Mundhe
  • 374
  • 7
  • 15
4
votes
4 answers

Stuck on a simple object literal scope / referencing issue

Here's a sample of my structure: this.is.a.really.long.namespace = { inputs : {}, buttons : {}, panels : {}, fn : { abc : function() {}, def : function() {} } }; Now, as you can see I'm storing my inputs,…
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
4
votes
2 answers

Javascript array/object syntax fail

Why do I get this error with this code: Uncaught SyntaxError: Unexpected token { on line 1. var cube_points = { {'x' : 100, 'y' : 100, 'z' : 100}, {'x' : 100, 'y' : 100, 'z' : -100}, {'x' : -100, 'y' : 100, 'z' : -100}, {'x' : -100,…
seymar
  • 3,993
  • 6
  • 25
  • 30
4
votes
2 answers

Performance of declaring json - JSON.parse vs object literal

According to the 2019 Chrome Dev Summit video, "Faster apps with JSON.parse", using JSON.parse with a string literal instead of declaring the json through an object literal results in a noticeable speed improvement. The google JSON.parse benchmarks…
Eugene
  • 10,957
  • 20
  • 69
  • 97
4
votes
3 answers

Sort array of objects

I have an array of object literals like this: var myArr = []; myArr[0] = { 'score': 4, 'name': 'foo' } myArr[1] = { 'score': 1, 'name': 'bar' } myArr[2] = { 'score': 3, 'name': 'foobar' } How would I sort the array so it…
katherine
  • 261
  • 4
  • 15
4
votes
0 answers

Why / how does JavaScript allow an object's property name to be set by the variable name when providing a variable value with no property name?

That question title was a mouthful, and this is very difficult to explain without an example: Given a variable: const string = 'test'; An object can be created where the variable is passed in as a property value without explicitly defining a…
Blake Mann
  • 5,440
  • 23
  • 37
4
votes
2 answers

What languages provide the use of object literals?

What languages provide the use of object literals? (Or in what languages could you easily emulate them?) Can you give a code example? Starting with the obvious javascript snippet: var someObj = { someProperty: 123, someFunction: function()…
ivo
  • 4,101
  • 5
  • 33
  • 42
4
votes
1 answer

Javascript - SyntaxError: Invalid or unexpected token - while creating object - Invisible character

I was reading the article on Javascript Objects (http://javascriptissexy.com/javascript-objects-in-detail/), so I copy-pasted the following code into my Notepad++ and ran it in Chrome(Version 55.0.2883.87 m). Upon opening the console, console…
urosc
  • 1,938
  • 3
  • 24
  • 33
4
votes
3 answers

Javascript 'normal' objects vs module pattern

Currently I'm developing a large scale Javascript app(single page) and I've searched around the web to find some best practices. Most projects use the module pattern so the objects doesn't pollute the global namespace. At this moment I use normal…
GuyT
  • 4,316
  • 2
  • 16
  • 30
4
votes
2 answers

Better performance with javascript using Array, Object literal or JSON?

I am having a dilemma as I need to choose the best performing option. What I have now is a simple array like: var array = [ '/index1.html', '/index2.html', '/index3.html' ]; this array would contain around 60 options only but as I need to…
devjs11
  • 1,898
  • 7
  • 43
  • 73
4
votes
3 answers

Test if key-name exists in JavaScript object literal

In the text adventure I am making, my object literals for the rooms look like this: room : { // some info, exits : { north : -1, east : "house", south : "forest", west : -1 } } and in my function to move around it says: if…
drenl
  • 1,321
  • 4
  • 18
  • 32
4
votes
3 answers

JavaScript constructors with Objects

I've started to try learn how to use JavaScript Objects more extensively and I've been attempting to write an Object in JavaScript that allows me to set some Constructor arguments on initialisation. I've got to say, there's quite a few different…
MackieeE
  • 11,751
  • 4
  • 39
  • 56
4
votes
1 answer

weird behavior in Chrome console

Take a look at the screenshot... The reason why I'm experimenting with this is because I did a simple Google search for how to check if a parameter is a function, and I found this... var getClass = {}.toString; ... function isFunctionA(object) { …
Hristo
  • 45,559
  • 65
  • 163
  • 230
4
votes
2 answers

JavaScript - passing an object literal as second arg to Object.create()

Referring to the JavaScript code snippet below, questions: Why does the object literal {item: {value: "foobar"}} behave differently when assigned to a variable (like in line 1) vs. when passed as an argument to Object.create() (like in line…
RBR
  • 999
  • 3
  • 13
  • 24
3
votes
6 answers

How to remove object properties with negative values, regardless of depth?

I'm looking for a way to remove object properties with negative values. Although an existing solution is provided here, it works only with no-depth objects. I'm looking for a solution to remove negative object properties of any depth. This calls for…
Emman
  • 3,695
  • 2
  • 20
  • 44