Understanding the scope of "this" keyword [duplicate] - javascript

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
What is the 'new' keyword in JavaScript?
(17 answers)
Closed 4 years ago.
I am having tough time understanding the scope of thisin Javascript.
I am following some tutorial on web but my instructor did something which he advised not to do when explaining b
for example at the start of the instruction he said
var person = "roht"
function whatIsThis() {
return this
console.log(this)
}
function variable (){
this.person = "max"
}
variable()
console.log(person)
whatIsThis()
When I run this.person it will attach value to the global object which is a bad practise (since this have global scope)
but later he himself this thing endless number of time when explaining creating a new object using fucntion constructor
function Person(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
var elie = new Person("james", "roger");
I am sure that I am missing something, can someone help me figure this out?

Related

Why does a function not have access to the properties of the object in which it was created? [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.
I was going through this article and couldn't understand their solution 1 to losing context:
let user = {
firstName: "John",
sayHi() {
alert(`Hello, ${this.firstName}!`);
}
};
// Problem:
setTimeout(user.sayHi, 1000); // Hello, undefined!
// Solution 1:
setTimeout(function() {
user.sayHi(); // Hello, John!
}, 1000);
In the second case, I understand that this works because the anonymous function callback gets access to a lexical environment populated with the globals (here, user) but then even user.sayHi() should have a similar lexical environment with access to the stuff between the surrounding {} (i.e. firstName) so we shouldn't need the wrapper in the first place. What am I missing here?

Outside scope this key word in JavaScript? [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 5 years ago.
I am not sure why I'm getting undefined with the code below. I tried to declare a variable under the say method var _this = this and then console.log out _this.name but it did not work.
let dog = {
name: 'doggo',
sayName() {
console.log(this.name)
}
}
let sayName = dog.sayName
sayName()
window.name="test";
sayName();//test
Executes the function in window context so this is window. You may want to keep the dog context either through passing it:
sayName.call(dog);//doggo
Or through keeping a bound function:
let sayName = dog.sayName.bind(dog);
sayName();//doggo

Is there a useful difference between defining a method in an object contructor and appending it to the constructor's prototype object? [duplicate]

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 7 years ago.
Consider this code example:
function Person(config) {
this.name = config.name;
this.age = config.age;
}
Person.prototype.getAge = function() {
return this.age;
};
var tilo = new Person({name:"Tilo", age:23 });
console.log(tilo.getAge());
Rather than defining getName() as an appendage to the constructor's prototype property, it seems to me it could be defined within the constructor to achieve the same thing? Is there any useful difference? In other words, does doing it one way or the other have any particular implementation advantage?
The constructor will create a new instance of that function with each invocation. Putting it on the prototype will save memory.

Does the way a JS function is defined affect it's "performance"? [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
Is there ANY difference in the following two ways of defining a functions?
METHOD 1)
var printName = function(name){
return("Hi! My name is ",name)
}
VS
METHOD 2)
function printName(name){
return("Hi! My name is ",name)
}
and I mean ANY, I'm new at JS and want to lay down my understanding of functions and Objects before I advance as I feel these 2 features are the 2 I'll use the most.
Yes there is a difference, but none that would affect the performance of the function code when it's called.
The difference has to do with when the function is created, but the performance is identical. Using your examples:
printName_1("Drew"); // This will fail, as printName_1 is not defined (yet)
printName_2("user4820485"); // This will work
var printName_1 = function(name){
return "Hi! My name is "+name;
}
function printName_2(name){
return "Hi! My name is "+name;
}
Functions that are declared using the latter syntax are initialized at the beginning of the block where they appear, so it looks like they can be called before they are defined.

use of "that" keyword in javascript [duplicate]

This question already has answers here:
What does 'var that = this;' mean in JavaScript?
(6 answers)
Closed 10 years ago.
I have few javascript that has used the keyword "that" extensively.
I see a lot of posts talking about the javascript keyword "this".
I wanted to understand the meaning of this key word in javascript context and it's visibility/scope.
Something like
that.someFunctionaName(someParameter)
What does it mean?
I understand the keyword "this" always points to the owner of the current object.
that is not a keyword in JavaScript. I suspect the code that you have is using something in the class to define an instance of itself. For example:
function myClass()
{
var that = this;
}
By doing this, you can ensure you're referencing the object, and not another element. For example, consider the following sample:
function myClass()
{
var that = this;
$('.myele').click(function() {
// 'this' refers to the element that was clicked.
// 'that' still refers to the myClass() object.
});
}

Categories

Resources