can you help me explain why this points to window
function fn(){
console.log(this)
};
principle
can you help me explain why this points to window
function fn(){
console.log(this)
};
principle
Brother, have you lost the step-by-step process? When we write it, we usually write it simply. Normally, for example, alert is also an attribute of window. It should be written as window.alert, just as this function is window.fn (), when it is called, but we generally write it as a direct call to fn (), that is to say, it is called by window, so this points to window, and remembers that whoever calls this points to ok
under the conditions you give, this
does not necessarily point to window
this
depends on how the fn
(function) is called. To sum up, there are three ways to call a function in javascript
.
declare a function first.
function fn(){
console.log(this);
}
the first type of , most commonly, is called as a property of an object, where the this
points to the object that called it.
var obj = {
go: fn
}
obj.go(); // thisobj
fn(); //
//
window.fn(); // this window
// nodejs
global.fn() // this global
the second , new keyword call, this points to the newly created object instance (the object's _ proto__
points to fn.prototype
), which is a little more complicated. You can take a look at reference document .
new fn();
the third , call and apply are called. this
can be specified by yourself.
var obj2 = {
name: 'segmentfault'
}
fn.call(obj2); // thisobj2
this belongs to bom and dom content
one or two sentences are not clear
I suggest that you systematically learn the basics of js (BOM DOM ECMAScript)
https://pan.baidu.com/s/1hqwt0R6
this objects are bound at run time based on the environment in which the function executes: in the global environment, this equals window, and the function is called as a method of an object, this equals that object.
function fn(){
alert(this)
};
fn();//window.fn(),thiswindow
var obj = {};
obj.fn2 = fn;
obj.fn2();//thisobj
how to determine the value of this
this refers to the execution environment of the current reference function refers to the object
in js, except that this refers to the formatting context of the current function execution function when writing the arrow function, the rest is determined during function execution. The question asked by the landlord seems to be less called.
if this is what the landlord wants to ask:
function fn () {
console.log(this)
};
fn ()
then in fact the function environment in which the fn function is called is global, that is, the window environment.
is window.fn () when actually called. So this is pointing to the window object
1.fn ();
windowthiswindow
var age = 38;
var obj = {
age: 18,
getAge:function () {
console.log(this.age);// 18
function foo(){
console.log(this.age);// 38
}
foo();//window
}
}
obj.getAge(); //
2. As a method obj.fn (), this pattern points to the object calling the method
var fn = function(){
console.log(this.length);
}
var arr = [fn, 1, 2];
arr[0](); // 3
3. In constructor mode, this points to the object coming out of new
function Person(){
console.log(this);
}
var p = new Person();
4. Context mode call () apply () bind ()
thisnullundefinedthiswindow
newcomers began to learn VUE, to emulate the example on the Internet to achieve an example of form generation and deletion, the actual process encountered a problem, in the html tag code I added an additional form tag will not be able to run normally, a...
html structure: <div class="box"> <div class="item">< div> <div>< div> < div> js section: $( .box ).on( mouseover , .item ,function () { $(this).stop().animate({ fontSize ...
the following data is pulled out of the interface (the data format is as follows), but the data in the "purpose " filter is also pulled out twice. How to insert the data pulled by the "purpose " into the 1. The first layer of data is rendered by this...
how to use the yapi interface management tool to write the data returned from the previous interface to the header of the following interface? For example, log in and get the token, and then the subsequent APIs need to add this token to the header ...
How do I drag and drop an element into another element box in HTML5, but only copy it into the element box? That is to say, this element still exists in the original place after dragging? ...
can the html object of the entire template be obtained only through the native js s document.getElementById ( box ). Content? Or does jquery have built-in methods? Baidu is less than asking for advice ...
the first search is that the data can be rendered normally, but there is a problem with the second this pointing I have tried to define a variable and pay this to this variable, but it still doesn t work <el-select v-model="seller&...
the first search is that the data can be rendered normally, but there is a problem with the second this pointing I have tried to define a variable and pay this to this variable, but it still doesn t work <el-select v-model="seller&...