why the code this point to window object?

2024/2/27 8:36:15

my code is:

var length = 20;
function fn(){console.log(this.length);
}var o = {length:10,e:function (fn){fn();arguments[0]();}

the output is 20,1,who can tell me why?


When the this keyword occurs inside a function, its value depends on how the function is called.

In your case, fn() is called without providing the a this value, so the default value is window. With arguments[0](), the context is the arguments object, whose length is 1.

The point is it does not matter where the function is called, but it matters how the function is called.

var length = 20;
function fn(){console.log(this.length);
}var o = {length:10,e:function (fn){fn(); // this will be the window.arguments[0](); // this will be arguments object.}

Further more, if you want this to be the object o, you could use call or apply, or bind an object first.

var length = 20;
function fn(){console.log(this.length);
}var o = {length:10,e:function (fn){var fn2 = fn.bind(this);fn.call(this); // this in fn will be the object o.fn.apply(this); // this in fn will be the object o.fn2(); // this also will be the object o.}

Related Q&A

Search button for Leaflet R map?

Im using the leaflet library in R, which is a wrapper to the leaflet.js library. Im wondering if it is possible to add a query or search button using the R interface (or some hack to the underlying cod…

Replace file when downloading

Referring to this example: http://jsfiddle.net/m9nts8r1/ How can I update the content of updatedText.xml when pressing the Download button, just as it does when the Save button in any application?In t…

Force Mime Type in Browser (with Javascript)

[I imagine that this is impossible due to security concerns, but:]Is it possible to force the browser to handle a file with a certain mime type without controlling the server? Im serving up files from…

AngularJS - File Download through AJAX

I created an angular js program for downloading a file from the server here follows the codeHTML Code<a download="fullList.csv" ng-href="{{ fullListUrl }}" type="button"…

Click event doesnt work in safari mobile for some HTML content

In my web app, there is a separate navbar for mobile devices. I want this navbar to collapse when the menu button is clicked or anywhere else in the site is clicked. It already works in any mobile brow…

IN Operator equivalence in indexedDB

I want to execute this query select * from properties where propertyCode IN ("field1", "field2", "field3")How can I achieve this in IndexedDB I tried this thinggetData : f…

Data Races in JavaScript?

Lets assume I run this piece of code.var score = 0; for (var i = 0; i < arbitrary_length; i++) {async_task(i, function() { score++; }); // increment callback function }In theory I understand that th…

How to set the state based on the local storage without overwriting it with the initial state?

I try to get the updated localstorage state called darkmode with useContext but when I console log it in functions inside App.tsx it gives the default value set inside Context.js : App.tsx: const { dar…

How to create a showdown.js markdown extension

Using the following code, I get working output:<html><head><script type="text/javascript" src="/js/showdown.js"></script></head><body><script …

DataTables: sort by numeric data-order not working?

I am using DataTables version 1.10. I want to make a column sortable by a numeric value, when the value shown in the column is not numeric.I can see that what I need to do is add a data-sort attribute …