Can I use a key in an object literal?

Possible duplicate:
Self-advertisements in object literal declarations

I have an object literal that is used as a configuration item and is looking for keys.

customRendering:{ key1: func(){....}, key2: func(){....} } 

I have a situation where key2 and key3 can use the same function. Is there a way to assign the same function to both key2 and key3 without having to declare a function outside the object literal and not declare an additional key?

I was hoping I could do something like:

 key2: key3: func(){....} 
+4
source share
4 answers

Safe way:

I do not know what I was thinking before. If you find it convenient to use a more verbose literal, you can create a custom function:

 o = new function () { this.foo = function () { console.log('works'); }; this.bar = this.foo; }; 

This is a dirty nasty hack:

You can use a temporary variable to store a function reference when setting the object. Be careful to use closure and invoke var before using it so that you don't pollute the global namespace:

 (function () { var o, baz; o = { foo: baz = function () {console.log('works')}, bar: baz } //more code }()); 

The reason I call it a dirty nasty hack is because the code makes it less readable, it’s more difficult to talk about this code (especially if the object literal declaration was larger), where baz was installed.

It’s better to just write an alias outside the object literal so that it explicitly displays that it is an alias.

Note. the default name format does not work:

 o = { //THIS WON'T WORK foo: function baz() {/* code */}, bar: baz } 

There is no way in an object literal to define an alias using a generic link.

You can use the aliases function, but it will not be an identical reference:

 o = { foo: function... bar: function () { return this.foo() } //even better if you use `apply` or `call` } 

A typical way to exchange a link is after an object literal, which sounds like something you wanted to avoid:

 o = { foo: function... } o.bar = o.foo; 

Alternatively, as you pointed out in your question (and for completeness), you can define a function outside the object literal:

 func = function () {/* code */}; o = { foo: func, bar: func } 

In response to @Peter about returning an object from a function

Using a self-executing anonymous function is another way to instantiate an inline object and will make this whole question moot:

 o = (function () { var o = { foo: function () {/*code*/} } o.bar = o.foo; return o; }()); 

Or like this:

 var o = (function () { var shared = function() { console.log("shared") }; return { foo: shared, bar: shared } }()); 
+9
source

without having to declare a function outside the object literal

I think this part makes it impossible. However, although I use JavaScript all the time, I am not a professional JS ninja, so maybe I'm wrong.

The code for how I would do this (although it seems you already know that you can do this, I thought it might be best to say this):

 customRendering = {}; customRendering.key1 = func(){....}; customRendering.key2 = customRendering.key1; 
+1
source

Define a function other than an object:

 var func = function() { ... }; ... customRendering: { key1: func, key2: func } 
-1
source

Sorry, I misunderstood your question.

Here is a possible solution:

  var obj = { func1: function() {}, func2: obj.func1 } 
-2
source

Source: https://habr.com/ru/post/1381772/


All Articles