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

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??

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.

What is altarnative of eval for strings in javascript [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
hi what can I use inspite of eval function for that code :
product_pcs = 12;
product_code = "lkb_12" ;
eval(product_code +"_pcs_1 = product_pcs");
alert (lkb_12_pcs_1);
I recommend you build an object instead
const products = {};
prodcuts['lkb_12'].pcs = 12;
prodcuts['lkb_12'].name = 'test';
prodcuts['lkb_13'].pcs = 14;
prodcuts['lkb_13'].name = 'test2';
// ...
``

Link to array push function [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
Basically why I can't do this:
var a = [];
var b = a.push;
b(1);
it give's error, I know how to avoid this, but why this is not working in js?
The this value is determined by how the function is called. You can use Function#bind to ensure the this value is set.
var a = [];
var b = a.push.bind(a);
b(1);
console.log(a);
Try this:
var a = [];
var b = (param) => a.push(param);
b(1);

Why is this not undefined? [duplicate]

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?

Define object by a variables string [duplicate]

This question already has answers here:
How do I make JavaScript Object using a variable String to define the class name?
(10 answers)
Using a variable value to call an array element
(7 answers)
Closed 8 years ago.
Hard to explain.. I basicly want to do the following:
var doWhat = "speak";
var speak = {
hello: function() { alert: "Hello!"; }
};
// Won't work
doWhat.hello();
It's a bad example, but you should be able to get what I mean.
Is it possible somehow ?
You can use eval(doWhat).hello();. That way the contents of doWhat will be evaluated to the object reference.
You can do something like
var doWhat = {}, str = "speak";
doWhat[str] = {
hello : function() {}
};
doWhat[str].hello();
jsName = 'PageIndexController';
//ST1
eval("if( typeof jsName === 'undefined')alert(111);");
//ST1
eval("if( typeof " + jsName + " === 'undefined')alert(222);");
//ST1 not work
//ST2 work and the output: 222;
//there are two different way using eval, we will get 2 different outcome.

Categories

Resources