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 defined property within an OBJECT LITERAL:
var foo = {
bar : 5,
baz : bar
}
alert (foo.baz) // I want 5, but evaluates to undefined
I know that I could do this:
var foo = {
bar : 5,
baz : function() {
alert(this.bar); // 5
}
But I want to assign baz directly to a value rather than a function. Any ideas?
No, you won't be able to use any properties of the object literal before it has been created. Your closest option is probably to use a temporary variable like so:
var tmp = 5,
foo = {
bar : tmp,
baz : tmp
}
If you are free to use ECMAScript 5 features, you could write a getter function for the baz property that instead returns the value of bar:
var yourObject = {
bar: 5
};
Object.defineProperty(yourObject, 'baz', {
get: function () { return yourObject.bar; }
});
You can also just build a literal by parts:
var foo = {bar:5};
foo.baz = foo.bar;
If you need to fit this inside an expression (instead of through multiple statements) you can try abusing the comma operator or you can make a helper function:
(Warning: untested code)
function make_fancy_object(base_object, copies_to_make){
var i, copy_from, copy_to_list;
for(copy_from in copies_to_make){
if(copies_to_make.hasOwnProperty(copy_from)){
copy_to_list = copies_to_make[copy_from];
for(var i=0; i<copy_to_list.length; i++){
base_object[copy_to_list[i]] = base_object[copy_from];
}
}
}
}
var foo = make_fancy_object(
{bar: 5},
{bar: ["baz", "biv"]}
);
//foo.baz and foo.biv should be 5 now as well.
Related
I am new to JS. While seeing this code:
var obj = {
foo : function foo(){console.log("foo");}
}
I was wondering why the 2 foo names did not create conflict ?
The first one is the key name, and the other one is the function name:
var obj = {
foo : function foo(){console.log("foo");}
// ^ ^
// Key Name
} | |
v |
obj.foo.name <-----+
// => foo
It is something similar to:
function foo () {console.log("foo");}
var obj = { foo: foo }
// or simply
var obj = { foo }
Function.name
A Function object's read-only name property indicates the function's name as specified when it was created, or it may be rather anonymous or ''(an empty string) for functions created anonymously.
I ran into some code that looks like the following:
const {
foo = []
} = this.options
Assuming in this case that this.options is an JavaScript Object, how does this work? Does all of this.options get assigned to foo and if this.options is undefined, does foo just get initialized to an empty array? I found this code confusing because this.options is not an Array but is instead an Object of key/val pairs.
Sometimes it helps to just try things out. What you'd observe is that a default value is assigned to foo in case it is missing within the to be assigned object
function one() {
const options = {};
const {
foo = []
} = options;
console.log(foo);
}
function two() {
const options = {foo: 'bar'};
const {
foo = []
} = options;
console.log(foo);
}
function three() {
const options = {};
const {
foo = 'foo',
bar = 'bar',
baz = 'baz'
} = options;
console.log(foo, bar, baz);
}
one();
two();
three();
From MDN (emphesis mine ) :
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Not all of this.options get assigned to foo, it's unpacking foo from the Object :
const options = {
foo: ['foo', 'bar'],
bar: 'hello world'
}
const {
foo = []
} = options;
console.log(foo);
And foo = [] is there to be a fallback to have an empty array if this.options does not have a property foo :
const options = {
bar: 'hello world'
}
const {
foo = []
} = options;
console.log(foo);
If this.options is ` undefined, you'll get errors,
options is not defined
const {
foo = []
} = options;
console.log(foo);
Or:
Cannot destructure property foo of 'undefined' or 'null'.
const options = undefined;
const {
foo = []
} = options;
console.log(foo);
If you run it through babel you'll see that if this.options.foo exists then it will be bound to the name 'foo' in that scope and if it doesn't then foo is set to an empty array.
Here is an in-depths article for you to better understand ES6 Destructuring https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/
Destructuring assignment allows you to assign the properties of an
array or object to variables using syntax that looks similar to array
or object literals. This syntax can be extremely terse, while still
exhibiting more clarity than the traditional property access.
For your sample script the assignment are looking for an object with property "foo" from right side. If it can not find it, it will assign foo with default value as empty array.
If the left side is null or undefined, the operator will throw an error "Uncaught TypeError: Cannot destructure propertyfooof 'undefined' or 'null'."
// A regular JS object.
this.options = {
'a': 'a',
'foo': 'bar',
'z': 'z'
}
console.log('this.options.foo =', this.options.foo);
// this.options.foo === 'bar'
// Get 'foo' out of this.options. Becomes undefined if 'foo' doesn't exist in this.options.
const { foo } = this.options;
console.log('foo =', foo);
// foo === this.options.foo === 'bar';
const { nope } = this.options;
console.log('nope =', nope);
// typeof nope === 'undefined'
// Get 'zzz' if it exists, otherwise fall back to 1234.
const { zzz = 1234 } = this.options;
console.log('zzz =', zzz);
// zzz === 1234;
// zzz was set by its default fallback value.
Is it possible to somehow compose a string dynamically? I've read a bit about pass-by-value and pass-by-reference, and thus I'm creating all the strings as objects.
Example:
var foo = {str: 'foo'};
var bar = {str: foo.str + 'bar'};
var baz = {str: bar.str + 'baz'};
foo.str = 'fuu';
console.log(baz.str); //expected 'fuubarbaz', got 'foobarbaz
Thanks in advance!
Nah, when you define things statically like that, they're going to use the variable when it was called. You could do something like this with getters though:
let foo = {str: 'foo'};
let bar = {get str() { return foo.str + 'bar'; }};
let baz = {get str() { return bar.str + 'baz'; }};
foo.str = 'fuu';
console.log(baz.str); // properly outputs `fuubarbaz`
The reason why this works is the magic of getters; instead of defining the property statically, you're defining a function that gets called when trying to access the property. This way it can "react" to any downstream changes, because it's always dynamically generated.
It doesn't work like this, the concatenation foo.str + was executed only once, the plus sign is not a function that is called multiple times.
One way to do what you want is create an object with 3 strings and a method!:
const obj = {
a: 'foo',
b: 'bar',
c: 'baz',
show: function() {
return this.a + this.b + this.c;
}
};
console.log(obj.show());
obj.a = 'fuu';
console.log(obj.show());
Based on puddi's answer I came up with this:
console.clear()
var foo = {
// _str is the storage of str
_str: 'foo',
// getter of str, always called when accessing str in a read context
get str() {return this._str},
// setter of str, always called when accessing str in a write context
set str(str) {this._str = str}
};
// read context, so get str() of foo is called
console.log(foo.str) // "foo"
var bar = {
// define getter function of bar, calls getter function of foo
get str() {return foo.str + 'bar'}
};
// read context, so get str() of bar is called
console.log(bar.str) // "foobar"
var baz = {
// define getter function of baz, calls getter function of baz
get str() {return bar.str + 'baz'}
};
// read context, so get str() of baz is called
console.log(baz.str) // "foobarbaz"
// write context, so set str(str) of foo is called. foo._str is now 'fuu', was 'foo'
foo.str = 'fuu';
// read context, getter of baz is called which calls getter of bar which calls getter of foo which returns _str which has the value of 'fuu'
console.log(baz.str); // "fuubarbaz"
Alternatively you can user Object.defineProperty:
console.clear();
var foo = Object.defineProperty({}, 'str', {
enumerable: true,
get: () => this._property_str,
set: (str) => this._property_str = str
});
var bar = Object.defineProperty({}, 'str', {
enumerable: true,
get: () => foo.str + 'bar',
});
var baz = Object.defineProperty({}, 'str', {
enumerable: true,
get: () => bar.str + 'baz',
});
foo.str = 'foo'
console.log(foo.str) // "foo"
console.log(bar.str) // "foobar"
console.log(baz.str) // "foobarbaz"
foo.str = 'fuu';
console.log(baz.str); // "fuubarbaz"
Given the code with a closure, why is make_function returning null when I pass it an object?
Plnkr: http://plnkr.co/edit/L3nNeiibTMdR6GEnyMOX?p=preview
$(document).ready(everything);
function everything() {
var null_start = null;
var object_start = { obj: null };
function make_function (arg) {
return function () {
d3.select('#console')
.append('p')
.text(JSON.stringify(arg))
.style('color', 'blue');
};
}
// Make a function with arg=null
var func = make_function(null_start);
d3.select('#console').append('p').text('Expecting null here');
func();
null_start = {
foo: 5,
bar: 42
};
d3.select('#console').append('p')
.html('Expecting {foo:5, bar:42} here <span style="color:red;">Why is this still null???</span>');
func();
// Make a function with arg={obj:null}
func = make_function(object_start);
d3.select('#console').append('p').text("Expecting {obj: null} here");
func();
object_start.obj = {
foo: 5,
bar: 42
};
d3.select('#console').append('p')
.html('Expecting {obj: {foo:5, bar:42}} here <span style="color:red;">if this one is updated?</span>');
func();
}
Arguments passed in JavaScript are not passed by reference, but by a special "reference-copy". In other words, when you pass null_start to make_function, a copy of the null_start variable is passed.
Changes to the null_start variable would not be reflected by the variable passed to the function.
This is the reason why creating another function with the new value of the variable works as intended in your example.
Note that when passing objects, changes to the object will be reflected by the original variable as well. This is because objects are mutable in JavaScript. For example:
function change (obj) { obj.foo = 'bar'; }
var x = {};
change(x);
console.log(x.foo); // 'bar'
This is because an object is merely a box of keys pointing to values. Keys inside the object point to values, which can be modified, and reflected upon by the outer-scope of the function. But if we pass the value directly, it will not work:
function change (val) { val = 'fff'; }
var x = {foo: 'bar'};
change(x.foo);
console.log(x.foo); // 'bar'
Hope that helps!
Can someone explain why if I add a property to foo, somehow bar also inherits that property when I do this:
var foo = bar = {};
foo.hi = 'hi';
console.log(bar); //Object {hi: "hi"}
How does this work? I'm setting properties on foo, not bar. I realized I passed the object to bar and then bar to foo, but I must be missing something here.
Integer assignment works differently and more sense (to me anyhow):
var foo = bar = 5;
foo = 4;
console.log(bar); // 5
Objects are passed by reference in JavaScript. Strings and number literals are not. In your code foo === bar, is the same object. You can simply declare the variables separately:
// Two different object instances
var foo = {};
var baz = {};
by doing foo = bar = {};, foo and bar are pointers to same object. so when you do:
foo.hi = 'hi';
It sets bar.hi also, because foo and bar point to the same object. To make it different, you should do:
var foo = {}, bar = {};
foo.hi = 'hi';