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
-2
votes
1 answer

Assigning to an object literal dynamically in Typescript

I have been at this for hours and cannot determine why I am getting this typescript error. I can't show the actual code, but I've reduced the problem down to the following example code: abstract class CustomerType { protected abstract…
Daryl
  • 15
  • 7
-2
votes
1 answer

Referencing a javascript object from within its declaration

Why is it possible to reference an object from within the object directly? For example: var object = { prop1 : "Prop 1", prop2 : "Prop 2", func : _ => console.log(object) } object.func(); //output {prop1: 'Prop 1', prop2: 'Prop 2',…
Austin737
  • 675
  • 1
  • 8
  • 15
-2
votes
1 answer

Does JavaScript have an object literal shorthand which automatically assigns to a property a value which is a string of the property name?

I understand that in ES2015+, instead of writing: let myObject = { a: "a", b: "b", c: "c" }; I can use object shorthand to write the following: let a = "a"; let b = "b"; let c = "c"; let myObject = { a, b, c }; console.log(myObject); But that…
Rounin
  • 27,134
  • 9
  • 83
  • 108
-2
votes
1 answer

How could I avoid using the name of an object property along with the this keyword in TypeScript?

How could I avoid using the name of an object property along with the this keyword in TypeScript? E.g. I can write: const foo = 2; const bar = 3; const baz = { foo, bar }; But I can not do the following: class A { foo: number = 2; bar:…
Sasuke Uchiha
  • 857
  • 2
  • 11
  • 23
-2
votes
1 answer

Javascript: Create an object with keys descending (numbers) order

I am trying to create an object that looks something like this: { '2017': 2017, '2016': 2016, '2015': 2015, '2014': 2014 } But for whatever reason, it keeps outputting me ascending order instead: const EXAMPLE = () => { const…
Alejandro
  • 2,266
  • 6
  • 35
  • 63
-3
votes
2 answers

How can i populate a complex literal object javascipt

I have 2 arrays( named countries and medals) with data that i need to put into one object literal in order to have the following output let countries= ['Korea', 'United States', 'Germany', 'Canada', 'Austria']; let medals = ['Gold medal', 'Silver…
Riley Christian
  • 149
  • 1
  • 13
-3
votes
2 answers

How to use template literal as key inside object literal?

How do I use template literal as key inside object literal? The following code: { `${id}_name`: "John Doe", `${id}_age`: 27, } Produces a syntax error: Uncaught SyntaxError: Unexpected token : Is there something I'm missing, or is this not…
David Callanan
  • 5,601
  • 7
  • 63
  • 105
-3
votes
1 answer

Implementing string method split() into the object literal w/ loop code

1st code: var str = "Hello"; var arr = str.split(""); var text = ""; var i; for (i = 0; i < arr.length; i++) { text += arr[i] + "
" } document.write(text); 2nd code: I want to implement the string method split() in the object…
lindsey
  • 1
  • 3
-3
votes
1 answer

Add a new property inside object without directly adding it?

I have this code: var a = { b: "Hello", c: "Foo", d: "Bar" }; if(a["b"]) { // this code will add a new property inside the object a } How can I add a new property without directly adding it inside the a object? I mean, adding a new property…
Mei
  • 31
  • 1
  • 3
-3
votes
2 answers

is this valid js oop?

I am wondering if it is okay to put html dom references in js oop. Although there are many JS OOP tutorials online, I haven't seen anything similar to what I am referring to. The code below is what I am referring to. var form = { fname :…
JaPerk14
  • 1,664
  • 3
  • 24
  • 32
-4
votes
1 answer

Compare two Object literal files value that uses specific variables and return the value if it has differences

I have two set of different Object literal color variables files. First one is main.js: const variables = [ { primary: { default: ['#fff', 'red', 'green', '#454545', '#333'], dark: ['blue', 'yellow', 'purple', 'orange',…
Karuppiah RK
  • 3,894
  • 9
  • 40
  • 80
-4
votes
4 answers

Object literal constructor

I am looking for a way to modify an object that was defined by a literal without passing it into another function. example: let o = {a:1,b:2} console.log(o.a===3) i thought when i define an object using a literal the the Object constructor would be…
user8023326
-4
votes
1 answer

Javascript in Object Literal approach difference between calling a method direct and using Object.create?

Suppose this is my object // Object Literal var personObjLit = { firstname : "John", lastname: "Doe", greetFullName : function() { return "personObjLit says: Hello " + this.firstname + ", " + this.lastname; } } I…
Piyush Jain
  • 1,895
  • 3
  • 19
  • 40
-5
votes
1 answer

Javascript accessing nested properties of an object literal

I did my best not to post a duplicate topic by doing a full search on Stack including some of the suggested Similar Questions on right side panel of this page but could not find a decent post that could help me. Here is a partial list of my search…
1 2 3
37
38