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 JavaScript features that you may not have used?

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

Share

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

Today, I will talk to you about the JavaScript function that you may not have used. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

JavaScript has many frequently used features. However, there are some features that most of us may not have used.

Tag template text

Template text with labels is a function for dealing with template strings.

They are often used in React libraries, such as style component libraries. This feature is actually very useful.

It makes it easy for us to parse the template string text. It allows us to use functions to parse template text.

For example, we can use it in the following ways:

Const tag = (strings,... vals) = > {console.log (strings); console.log (vals);} const name = 'foo';tag `Hi ${name} `

In the above code, we have the he tag function and the vals array, which has a string function that contains various parts of the string, which has values interpolated into the string.

In the above example, we have an array where 'Hi' and' 'are the values of string.

In the vals array, we have 'foo'.

We can use it to return the object to be returned. For example, we can rewrite the tag function as follows:

Const tag = (strings,... vals) = > {return `${strings [0]}! ${vals [0]}`}

In the above code, we take the various parts of the string from string and vals and put them in!. Between two expressions.

And then we get hi! Returns the foo of the value.

Comma operator

The comma operator always returns the last item in the list of items separated by the comma operator.

For example, we can use it in the following ways:

Const foo = (1,2,3)

And then we get 3.

It can use any expression and always returns the last evaluated expression.

With

The with operator is definitely an operator that we should not use. Use is prohibited in strict mode.

This operator adds some performance and security issues to the language. It is used to extend the scope chain of the statement.

Its usage is as follows:

With (expression) statement

Or:

With (expression) {statement statement...}

In the above code, we created a new scope around expression.

All statements in the block have the expression as the parent scope.

For example, we can use it in the following ways:

Const obj = {a: 1, b: 2, c: 3} with (obj) {console.log (a, b, c);}

In the above code, the expressions within the with block are scoped relative to the obj object.

So, an obj.a,obj.b b and c are actually obj.a,obj.b and obj.c.

As with with, block scope variables are not available outside the with block.

For example, if we have:

Const obj = {a: 1, b: 2, c: 3} with (obj) {console.log (a, b, c); let x = 1;} console.log (x)

If you try to call console.log on x, an error occurs when referencing x outside the with block.

In

The in operator is useful for checking whether the property is part of an object or any prototype in the prototype chain.

Returns true if the property is in the object or any property in the prototype chain, false otherwise.

For example, if we have the following class structure and objects:

Class Foo {constructor () {this.a = 1;}} class Bar extends Foo {constructor () {super (); this.b = 2; this.c = 3;} const bar = new Bar ()

Then, if we record the return value of the following expression:

Console.log ('a'in bar); console.log ('b' in bar); console.log ('c'in bar)

We think all the logs are correct.

This is because bar takes a Foo instance as its prototype, and the in operator checks the properties of the object itself and its prototype.

So the same is true of in bar. If we only want to check whether the property is non-inherited, we must use the obj.hasOwnProperty method, where obj is any JavaScript object without a null stereotype.

The template text of the tag can be used to convert the template string to the desired value.

The comma operator always returns the last item in the comma-separated list.

The in operator checks whether the property is in the object or its prototype.

After reading the above, do you have any further understanding of the JavaScript features that you may not have used? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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