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
Related
This question already has answers here:
this inside function
(6 answers)
Closed 3 years ago.
$("btn").click(function(){
function(){
console.log(this);
}
})
Hi All,
where "this" is refering in this piece of code?A interviewer asked me,i got confused.Please suggest.Thanks
this keyword refers to an object, that object which is executing the current bit of javascript code.
In other words, every javascript function while executing has a reference to its current execution context, called this. Execution context means here is how the function is called.
To understand this keyword, only we need to know how, when and from where the function is called, does not matter how and where function is declared or defined.
function bike() {
console.log(this.name);
}
var name = "Ninja";
var obj1 = { name: "Pulsar", bike: bike };
var obj2 = { name: "Gixxer", bike: bike };
bike(); // "Ninja"
obj1.bike(); // "Pulsar"
obj2.bike(); // "Gixxer"
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?
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.
This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 8 years ago.
Is there a different outcome (scope etc...) between the following two function declarations?
var myObj = {
foo: function myName() {}
}
var myObj = {
foo: function() {}
}
No, the scope is the same. In javascript anonymous functions can be named and its good practice to do so because when debugging they are named in stack traces. Also you can refer to the function inside itself. More details here.
when you give a name to the function, it's only available from inside that function
var myObj = {
foo: function myName() {
myName()
}
}
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.
});
}