In JavaScript, why should you use local variables whenever possible?

In JavaScript, why should you use local variables whenever possible?

If you don’t do this, how much loss will it cause to performance? This article will explore the answers to these questions and fundamentally understand what factors are related to the reading and writing performance of variables. Copyright Statement This article was translated from Nicholas C. Zakas on personal website< on February 10, 2009 "Javascript Variable Performance” published on /FONT>. Original text is the only official version. This article is a Simplified Chinese Translation authorized by the original author (Nicholas C. Zakas). The translator (明达) has made a lot of efforts to ensure the accuracy of the translation, and promises that the content of the translation is completely faithful to the original text, but it may still contain omissions and inaccuracies. Welcome everyone Correction. The content of the translation notes is informal and represents only the translator’s personal views. The following is a translation of the original text: On the issue of how to improve the performance of Javascript, the most commonly heard suggestion is to use local variables instead of global variables. This advice has always stuck with me and never been questioned in my nine years of working in web development, and the basis for this advice comes…

JavaScript - The difference between let and var

In JavaScript, closures are used to solve the problem of only getting the last value of any variable in a function.

javascript Write your review! Come on, watch it all Member login | User registration Recommended reading javascript [JavaScript] A pitfall about console.log() that most front-end engineers have not noticed [JavaScript] A pitfall about console.log() that most front-end engineers have not noticed. Please read the following code and guess the result: functiontest(){le… [detailed] Crayon Shin-chan 2023-10-13 19:00:52 io JavaScript – The difference between let and var The premise is that ES5 only has function scope and global scope, and var belongs to ES5. let belongs to ES6 and has a new block-level scope. The goal is to write safer code. Theletstatementdeclaresablocks… [detailed] Crayon Shin-chan 2023-10-13 18:33:54

In JavaScript, why null==0 is false and null greater than=0 is true (personal research)

In life, we are constantly writing code and writing Javascript, and we rarely have time to conduct conceptual research. As for me, I have nothing to do today, so I studied the relationship between “null” and “0”. I hope everyone can gain something from reading it. The code is as follows: alert(null>=0) The code is as follows: What will pop up in the above code? False? True? In fact, it is true. So why? Why is “null>=0” true? When null>=0, it is forced to a numeric type. When comparing null>=0, it is the answer obtained by comparing null<0. If a=b is false, if a=b is true, that is, 0<0 is false, that is, null0 is true. So null>=0 is true. The code is as follows: alert(null==0) What will pop up in the above code? False? True? Actually it is false. “null==0” is treated specially and will not be converted to a numeric type or a numerical value. However, if the left side is a string and the right side is a numerical value, it will be converted. “null” is an object (empty object, without any properties and methods). And “0” is a number. As mentioned before, “==” does not convert…

In JavaScript, why do Math.max() and Math.min() return INFINITY and INFINITY without filling in parameters?

In a widely circulated Javascript Meme (meme), we can see such an example, Math.max() returns without filling in the parameters-INFINITY, Math.min() returned INFINITY in the same situation. It is obviously a method to find the maximum number, but it returns -INFINITY used to represent the minimum number, which makes me feel a bit weird. It is actually very simple to find out why. The code of the standard library is actually written by people. Just look for Math.max() and Math.min() You will know the specific implementation method. The following text content is recommended to be viewed with the attached code Take Math.max() as an example. In the source code of the Javascript engine used by the Chrome browser, which is the Math library of the V8 engine, we can see that when the parameters are filled in When the number is 2, the function of returning the maximum value can be achieved by using the if statement. But when the number of parameters filled in is not 2, use for to loop through all parameters and use a variable >r stores the smallest value among the parameters. The variable r is assigned the value -INFINITY at the beginning. If the…

Explanation of the differences between “=, ==, ===” in JavaScript

= is an assignment operation, == is used for general comparison, and === is used for strict comparison == Data types can be converted during comparison; === Strict comparison, returns false as long as the types do not match. Example: “1” == true The types are different, “==” will perform type conversion first, and convert true to 1, which is “1” == 1; At this time, the types are still different. Continue to perform type conversion and convert “1” to 1, which is 1 == 1; At this time, the types on the left and right sides of “==” are both numerical, which is relatively successful! If compared: “1” === true, the left side is character type, the right side is bool type, the left and right sides have different types, the result is false; If compared: “1” === 1 The left side is character type, the right side is int numeric type, and the left and right sides have different types, the result is false; If compared: 1 === 1 The left side is of int numerical type, the right side is of int numerical type, the left and right sides have the same type and the same numerical…

In JavaScript, how to elegantly create a list of differences of array elements?

I have a list of numbers, say numbers = [3,7,9,10], and I want to have a list containing the difference between adjacent elements – in the given case , the element must have one less diffs = [4,2,1] Of course, I could create a new list, iterate over the input list and compile the results manually. I’m looking for an elegant/functional (not to mention pythonic) way to do this. In Python you would write [j-i for i, j in zip(t[:-1], t[1:])] for this or use numpy . reduce() Is there also a /list comprehension method in Javascript? 1> Nina Scholz..: You can slice and map differences. var numbers = [3, 7, 9, 10], result = numbers.slice(1).map((v, i) => v – numbers[i]); console.log(result);

In javascript, if you delete duplicate elements in a two-dimensional array

I’ve been trying for a long time, but I still haven’t come up with a solution. Please give me some ideas. [Ctrl+A to select all Note: Introducing external Js requires refreshing the page to execute]

In JavaScript, is it possible to bypass setters?

I have a o prototype object p: var p = {} var o = Object.create(p) You can add attributes o to the a object, and then add a setter with the same name to the prototype p: o.a = 1 Object.defineProperty(p, ‘a’, {set: function(value) {}}); console.log(o.a); // 1 However, if we try to add a property after the setter, it is not added to o – instead setter: is called Object.defineProperty(p, ‘a’, {set: function(value) {}}); o.a = 1 console.log(o.a); // undefined Is it possible to define the setter first and then bypass it to a add the attribute o directly?

In JavaScript, what happens if I assign to an object property that has a getter but no setter?

In the following code, both use console.log(o.x) to print 1. What will happen to the task o.x = 2? Has it been ignored? var o = { get x() { return 1; } } console.log(o.x); // 1 o.x = 2 console.log(o.x); // 1 CertainPerfo.. 5 In sloppy mode, yes, it will just be ignored – the “assigned” value will be discarded. But in strict mode (recommended), the following error will be thrown: Uncaught TypeError: Cannot set property x #Only one getter ‘use strict’; var o = { get x() { return 1; } } console.log(o.x); // 1 o.x = 2 1> CertainPerfo..: In sloppy mode, yes, it will just be ignored – “allocated” The value of will be discarded. But in strict mode (recommended), the following error will be thrown: Uncaught TypeError: Cannot set property x #Only one getter ‘use strict’; var o = { get x() { return 1; } } console.log(o.x); // 1 o.x = 2

In Javascript, is {}!=={} guaranteed?

Edit: My question is different from the one you linked. Specifically, it doesn’t say whether the JS virtual machine can reuse objects. In Javascript, are two objects created consecutively (certainly not by assigning one to the other) guaranteed to have different references? Consider the following code snippet: let a = {}; let b = {}; // b can be created at any time after a is created let areEqual = a === b; // comparison can be done at any time after b is created Is it guaranteed that areEqual is false? Some processor optimizations don’t b reuse references from a? 1> joshrathke..: Yes! Triple Equals is a combination of Value&Type. When you compare non-primitive variables in Javascript, you What is actually compared is their reference IDs. In other words, in your example a will never be equal to b, nor will there be any number of processor optimizations.

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索