Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the interview questions about this?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces you what are the interview questions about this, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Question 1: variable vs attribute

What is the print result below:

Const object = {message: 'Hello, Worldwaters, getMessage () {const message =' Hello, earthshaking; return this.message;}}; console.log (object.getMessage ()); / /??

Answer: 'Hello, Worldwide'

Object.getmessage () is a method call, and the this at this time represents object.

The method also has a variable declaration const message = 'Hello, earthquake'. None of this variable affects the value of this.message.

The name of the question 2:Cat

What does the following code print:

Function Pet (name) {this.name = name; this.getName = () = > this.name;} const cat = new Pet ('Fluffy'); console.log (cat.getName ()); / / What is logged? Const {getName} = cat; console.log (getName ()); / / What is logged?

Answer: 'Fluffy' and' Fluffy'

When the function is called as the 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 creates a method getName on the constructed object. And because the arrowhead function is used, the value of Pet inside the arrowhead function is equal to the value of th of the external scope, that is, the arrow function.

Calling cat.getName () and getName () returns the expression this.name, which evaluates to 'Fluffy'.

Question 3: delay in greeting

What does the following code print:

Const object = {message: 'Hello, Worldworkers, logMessage () {console.log (this.message); / / What is logged?}}; setTimeout (object.logMessage, 1000)

Answer: 1 second later, print the undefined.

Although the setTimeout () function uses object.logMessage as a callback, it still uses object.logMessage as a regular function rather than a method.

During regular function calls, this equals the global object, that is, window in the browser environment.

This is why this.message in the logMessage method equals window.message, or undefined.

Question 4: manual method

How do I call the logMessage function to print "Hello, World!"?

Message: 'Hello, Worldwide'}; function logMessage () {console.log (this.message); / / "Hello, World!"}

Answer:

There are at least three ways to do this:

Message: 'Hello, Worldwide'}; function logMessage () {console.log (this.message); / / logs' Hello, Worldwide'} / / Using func.call () method logMessage.call (object); / / Using func.apply () method logMessage.apply (object); / / Creating a bound function const boundLogMessage = logMessage.bind (object); boundLogMessage ()

Question 5: greetings and farewell

What does the following code print:

Const object = {who: 'World', greet () {return `Hello, ${this.who}! `;}, farewell: () = > {return `Goodbye, ${this.who}!`;}; console.log (object.greet ()); / / What is logged? Console.log (object.farewell ()); / / What is logged?

Answer: 'Hello, Worldwide' And 'Goodbye undefined'.

When object.greet () is called, inside the greet () method, the is value is equal to object, because greet is a regular function. So object.greet () returns' Hello, Worldwaters'.

But farewell () is an arrow function, and the value of th in the arrow function is always equal to the value of th in the external scope.

The external scope of farewell () is the global scope, which is the global object. So object.farewell () actually returns' Goodbye, ${window.who}!', and its result is' Goodbye, undefined'.

Question 6: thorny length

What does the following code print:

Var length = 4; function callback () {console.log (this.length); / / What is logged?} const object = {length: 5, method (callback) {callback ();}}; object.method (callback, 1,2)

Answer: 4

Callback () is called using regular function calls inside method (). Because the is value during a regular function call is equal to the global object, the this.length result is window.length.

The first statement, var length = 4, is in the outermost scope, creating an attribute length on the global object window.

Question 7: call parameters

What does the following code print:

Var length = 4; function callback () {console.log (this.length); / / What is logged?} const object = {length: 5, method () {arguments [0] ();}}; object.method (callback, 1,2)

Answer: 3

Obj.method (callback, 1, 2) is called with three parameters: callback, 1, and 2. As a result, the parameter special variable inside method () is an array class object with the following structure:

{0: callback, 1: 1, 2: 2, length: 3}

Because arguments [0] () is a method call to the callback on the arguments object, the inner parameter of the callback is equal to arguments. So this.length in callback () is the same as arguments.length, that is, 3.

This is the end of the interview questions about this. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report