In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the interview questions about this". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the interview questions about this"?
Note: the following JavaScript code runs in non-strict mode.
1. Variables and attributes
What does the following code output:
Const object = {message: 'Hello, Worldwaters, getMessage () {const message =' Hello, earthshaking; return this.message;}}; console.log (object.getMessage ()); / / = >?
Answer:
Output: 'Hello, Worldwide'
'object.getMessage () is a method call, which is why the this in the method is equal to object.
There is also a variable in the method that declares const message = 'Hellodepartment Earthparts, which does not affect the value of this.message.
two。 The name of the cat
What does the following code output:
Function Pet (name) {this.name = name; this.getName = () = > this.name;} const cat = new Pet ('Fluffy'); console.log (cat.getName ()); / / = >? Const {getName} = cat; console.log (getName ()); / / = >?
Answer: output: 'Fluffy' and' Fluffy'
When a function is called as a constructor (new Pet ('Fluffy')), the this inside the constructor is equal to the constructed object.
The this.name = name expression in the Pet constructor creates a name property on the constructed object.
This.getName = () = > this.name this.getName = () = > this.name creates the method getName on the constructed object. Because the arrow function is used, the this in the arrow function is equal to the this in the external scope, which is the constructor Pet.
Calling cat.getName () and getName () returns the expression this.name, which results in 'Fluffy'.
3. Delayed output
What does the following code output:
Const object = {message: 'Hello, Worldworkers, logMessage () {console.log (this.message); / / = >?}}; setTimeout (object.logMessage, 1000)
Answer:
After 1 second delay, output: undefined
Although the setTimeout () function uses object.logMessage as a callback, it still uses object.logMessage as a regular function rather than a method call. And this is equal to the global object in regular function calls and window in the browser environment. This is why console.log (this.message) in the logMessage method outputs window.message, which is undefined.
Challenge: how to modify this code to output 'Hello, Worldwide'? Write down your solution in the comments below *
4: complete the code
Complete the code so that the result outputs "Hello,World!".
Const object = {message: 'Hello, Worldwide'}; function logMessage () {console.log (this.message); / / = > "Hello, World!"} / / Write your code here...
Answer:
There are at least three ways to call logMessage () as a method on an object. Any one of them is seen as the correct answer:
Const object = {message: 'Hello, wordstones'}; function logMessage () {console.log (this.message); / / use the func.call () method logMessage.call (object); / / use the func.apply () method logMessage.apply (object); / / use function binding const boundLogMessage = logMessage.bind (object); boundLogMessage ()
5: greetings and farewell
What does the following code output:
Const object = {who: 'World', greet () {return `Hello, ${this.who}! `;}, farewell: () = > {return `Goodbye, ${this.who}!`;}; console.log (object.greet ()); / / = >? Console.log (object.farewell ()); / / = >?
Answer:
Output: 'Hello, Worldwide' And 'Goodbye, undefined'
When object.greet () is called, the value of this inside the method greet () is equal to object, because greet is a regular function. So object.greet () returns' Helloje Worldwaters'.
But farewell () is an arrow function, so the value of this in the arrow function is always equal to the this of the external scope. The external scope of farewell () is the global scope, where this is the global object. So object.farewell () actually returns' Goodbye, ${window.who}! 'and the result is' Goodbye, undefined'.
6. Tricky length
What does the following code output:
Var length = 4; function callback () {console.log (this.length); / / = >?} const object = {length: 5, method (callback) {callback ();}}; object.method (callback, 1,2)
Answer:
Output: 4
Use the regular function call inside method () to call callback (). Because the this value during a regular function call is equal to the global object, this.length is window.length in the callback () function.
The first statement at the outermost layer, var length = 4, creates the attribute length on the global object, so window.length becomes 4.
Finally, inside the callback () function, the value of ``this.length is window.length, and the final output is 4`.
7. Call parameter
What does the following code output:
Var length = 4; function callback () {console.log (this.length); / / output what} const object = {length: 5, method () {arguments [0] ();}}; object.method (callback, 1,2)
Answer: output: 3
Obj.method (callback, 1, 2) is called with three parameters: callback, 1, and 2. As a result, the arguments special variable in method () is an array-like object with the following structure:
{0: callback, 1: 1, 2: 2, length: 3}
Because arguments [0] () is a method call to callback on the arguments object, the this inside callback is equal to arguments. As a result, the this.length inside callback () is the same as arguments.length, both 3.
Thank you for your reading, the above is the content of "what are the interview questions about this". After the study of this article, I believe you have a deeper understanding of what the interview questions about this are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.