Why is this not undefined? [duplicate] - javascript

This question already has answers here:
Why is a global `name` variable declared in typescript and can I avoid using it?
(1 answer)
Is variable called "name" always defined in Javascript?
(2 answers)
Closed 4 years ago.
I have this code:
const employee = {
name: 'Jen Baker',
getName: function() {
return this.name;
}
};
const getEmployeeName = employee.getName;
console.log(getEmployeeName()); // returns '' (empty string)
Why does getEmployeeName() return an empty string? Shouldn't it be undefined because it has lost reference to this.name in the employee object? Where did the empty string come from?

Related

Odd syntax for setting an object's property in JavaScript [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.

Why output is different in "()=>{}" and "function () {}" function [duplicate]

This question already has answers here:
JavaScript ES6: Test for arrow function, built-in function, regular function?
(10 answers)
Arrow Functions and This [duplicate]
(5 answers)
Closed 4 years ago.
I thought the output would be same in ()=>{} and function(){} but I got a different output.
With function(){}
var person = {};
person.name = 'egoing';
person.introduce = function(){
return 'My name is '+this.name;
}
document.write(person.introduce());
the output was
'My name is egoing'
But with ()=>{} function,
var person = {};
person.name = 'egoing';
person.introduce = person.introduce=()=>{
return 'My name is ' +this.name}
document.write(person.introduce());
output was
'My name is '
Why is it differnt??

Destructing assignment with dots in property [duplicate]

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
Closed 5 years ago.
I have an object like this:
const myObject = { 'docs.count': 1000, uuid: 11244, 'pri.store.size': 2453 }
I would like to do a destructuring assignment. Is that only possible for this type of fields?
const { uuid } = myObject;
Thanks!
Variable names can't include a dot, so you can't get do const docs.count = 1000, for example. Destructuring allows you to extract the values even if the property name can't be a the name of a variable, but you'll need to assign them a valid variable name:
const myObject = { 'docs.count': 1000, uuid: 11244, 'pri.store.size': 2453 }
const { 'docs.count': docsCount } = myObject;
console.log(docsCount);

Why can't a variable be set to a <number>.toString(), but you can can call toString() on a return value? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 6 years ago.
function s(e) {
return e.toString();
}
var a = s(3); // works
var b = 3.toString(); // error
For example, set var a to the return value of s() that returns the first argument.toString(), but you can't set var b to 3.toString()
Javascript is expecting number(s) after the decimal, you can still do this, but you need to put your number in parenthesis:
(3).toString() = "3"

Creating an object with dynamic keys [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Here is a function building an object dynamically:
function onEntry(key, value) {
console.log(key) // productName
console.log(value) // Budweiser
const obj = { key: value }
console.log(obj) // { key: "Budweiser" }
}
Expected output is
{ productName: "Budweiser" }
But property name is not evaluated
{ key: "Budweiser" }
How to make property name of an object evaluated as an expression?
Create an object, and set its key manually.
var obj = {}
obj[key] = value
Or using ECMAScript 2015 syntax, you can also do it directly in the object declaration:
var obj = {
[key] = value
}

Categories

Resources