I am newbie to JavaScript. Here's the sample code.
let options = {
title: "Menu",
width: 100,
height: 200
};
console.log(options.title);
let {title, width, height} = { options:title, options:width, options:height}; // clause 1
I am getting Uncaught ReferenceError: Cannot access 'title' before initialization on clause 1.
Why? The object options is initialized and hence i was able to print the options.title.
Again. Sorry for asking stupid question.
Thanks.
In
let {title, width, height} = { options:title, options:width, options:height}, theoptions: titlepart is trying to create an object property calledoptions, setting its value using an in-scopetitlevariable. The onlytitlevariable in your code is the one being defined by thatlet. The initializer on the right-hand side of that=is evaluated when thetitlevariable has been declared but before it's initialized (ready to be used), so you get that reference error.It's like doing this, which tries to use
valueafter it's declared (becauseletdeclarations are hoisted to the top of their block) but before it's initialized (becauseletdeclarations aren't initialized until they're reached in the step-by-step execution of the code):I suspect you just wanted to use
options, so you got those values from the object you assigned tooptionsearlier in the code:Alternatively, from your comment, you may have meant to use
options.title(note the., not:) as atitlein the object literal:That also works (though there's no need for that object literal).