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.
Related
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"
The new object rest/spread syntax has some surprisingly nice applications, like omitting a field from an object.
Is there a (proposed) way to also assign to several properties of an object, the values from variables with the same names? In other words, a shorter way to say:
o.foo = foo;
o.bar = bar;
o.baz = baz;
Note: Without losing the existing properties of o, only adding to them.
Use Object.assign:
const o = { initial: 'initial' };
const foo = 'foo';
const bar = 'bar';
const baz = 'baz';
Object.assign(o, { foo, bar, baz });
console.log(o);
Note that both shorthand property names and Object.assign were introduced in ES6 - it's not something that requires an extremely up-to-date browser/environment.
Something similar that reassigns the reference to the object would be to initialize another object by spreading o and list foo, bar, baz:
let o = { initial: 'initial' };
const foo = 'foo';
const bar = 'bar';
const baz = 'baz';
o = { ...o, foo, bar, baz };
console.log(o);
const foo = 'foo';
const bar = 'bar';
const baz = 'baz';
const o = {foo, bar, baz};
console.log(o);
I have an object that wraps some data:
function Obj1() {
var _foo = 'bar'
this.obj2 = {
'b': 'c'
}
this.method = function() {
return _foo
}
}
var obj1 = new Obj1()
Now when I call console.log(obj1); I want it to show me object obj2 content. The trick is that I need to still be able to call obj1.method and get value of _foo. How do I do that if it's even possible?
My thought was that sth like getter will be suitable, but can't figure out where and how to assign one.
As far as I understood you're trying to hide method property. To achieve this, use Object.defineProperty. Function will not be logged because enumerable property is false by default which prevents property from showing in console.log for example.
function Obj1() {
var _foo = 'bar'
this.obj2 = {
'b': 'c'
}
Object.defineProperty(this.obj2, 'method', {
value: function() {
return _foo;
}
});
return this.obj2;
}
var obj1 = new Obj1()
console.log(obj1);
console.log(obj1.method());
if i understand correctly, you can use prototype
Example
function Obj1() {
this.obj2 = {
'b': 'c'
}
}
Obj1.prototype.method = function() {
return 'bar';
}
var obj1 = new Obj1();
//prints only properties
console.log(obj1);
//prints method result
console.log(obj1.method())
Since you calling new Obj1(). The result variable var obj1 is a class object and not a function, for you to get the value of obj2 you will have to call obj1.obj2 in your console log. If you want obj1 to hold the value of obj2. Then use the following code
function Obj1() {
var obj2 = {
'b': 'c'
}
return this.obj2;
}
var obj1 = Obj1();
console.log(obj1);
This will give you the required result in the console log, but the object will no longer be a class object and will have only the value of obj2.
Sticking to your original snippet a factory looks like a good option:
function factory() {
var foo = 'bar';
var props = { b: 'c'};
var proto = {
method: function() { return foo; }
};
var obj = Object.create(proto);
Object.assign(obj, props);
return obj;
}
var obj = factory();
console.log(obj); // {b: 'c'}
console.log(obj.method()) // 'foo'
You could even pass props as an argument to get a more flexible way of spawning objects with an "unenumerable" method accessing private members.
So I'm using an object to pass in my optional variables like so:
var foo = function (options) {
var options = options || {}; //provide a default, so that
var bar = options.bar || 42; //this doesn't fail on 'foo();'
//do stuff
};
JSLint complains that I'm overwriting options, which is what I want if it's falsy. Actually, I should probably check if it's an Object and elsewise throw an error to let the user know what's up. So - on that note - what would be good form here? Anybody know a good read on this? Additionally, how would I go about writing a bunch of functions with this pattern in a DRY style?
First, you need to not reassign the options argument to a var. Second, be careful with this for certain values:
var foo = function (options) {
options = options || {};
var bar = options.bar || 42;
var baz = options.baz || 'fubar';
};
foo({ bar: 0, baz: "" });
Inside foo, bar == 42 & baz == 'fubar' because 0 and "" are falsy. It's probably better to be more verbose so as to be more precise:
var foo = function (options) {
options = options || {};
var bar = typeof options.bar == 'undefined' ? 42 : options.bar;
var baz = typeof options.baz == 'undefined' ? 'fubar' : options.baz;
};
But, to be DRY, you can create a defaults object and just extend both objects to a settings object:
// generic shallow object extension, mutates obj1
var extend = function (obj1, obj2) {
for (var prop in obj2) {
if (obj2.hasOwnProperty(prop)) {
obj1[prop] = obj2[prop];
}
}
return obj1;
};
var foo = function (options) {
var defaults = {
bar: 42,
baz: 'fubar'
},
settings = extend(defaults, options);
};
Since you won't be using defaults any more in this invocation it doesn't matter that it gets mutated. Using this method:
foo(); // -> settings = { bar: 42, baz: 'fubar' }
foo({ bar: 0 }); // -> settings = { bar: 0, baz: 'fubar' }
foo({ bar: 0, baz: "" }); // -> settings = { bar: 0, baz: '' }
Remove the var from the options line and JSLint will stop complaining.
It's because you have a parameter called options, and then you are trying to declare a local variable called options. You can still set the default value if it's falsey
options = options || {};
To be honest, the null coalescing in JavaScript is pretty fine on it's own.
You can remember that you can be a bit 'exotic' with it, if you really want. So, for your example, if you just wanted options.bar or 42:
(options || {})["bar"] || 42
(remembering that you can access JavaScript properties by dot notation or array style)
It's not pretty - but it's just one example. Alternatively, you could do something like:
(options || {"bar":42})["bar"]
If you had a default set of options, you can do:
(options || defaults)["bar"]
Note : As mentioned in a different answer, you should be careful before certain values are falsely that you might not realise (i.e. an empty string). Of course, sometimes this is exactly what you want but it's just something to remember :)
JSLint is only complaining because you are using the var keyword, but options has already been defined. You can just remove the var and it won't complain.
To test if options is an object you can use typeof options === 'object', but note that arrays and null are also objects, so you may want to test something like:
typeof options === 'object' && options !== null && !(options instanceof Array)
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.