In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the variables and scopes in JavaScript, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
1. Original value and reference value
The values of the six simple data types are all original values, and when the original value is assigned to another variable through a variable, a new value is copied, which is independent of each other.
Let num1 = 5let num2 = num1
When a reference value is assigned to another variable through a variable, it also copies a value, which is actually a pointer (reference) that points to the same object.
Let obj1 = new Object () let obj2 = obj1
Since it points to the same reference object, adding properties to obj1 will also work on obj2.
Obj1.name = "zhangsan" console.log (obj2.name) / / zhangsan
In the process of passing parameters to a function, there is only one case in which it is passed according to the value, which is the same as the assignment of a variable. However, for reference values, the value passed is a pointer, but the pointer points to the same object.
2 、 instanceof
Typeof is generally suitable for judging original values, but not for reference values, because null and other objects return object, and you don't know exactly what type of object it is. The best way to determine what type of object the reference value is is to use instanceof expressions.
Console.log (1 instanceof Object) / / falselet obj = new Object (); console.log (obj instanceof Object) / / trueconsole.log (null instanceof Object) / / falsefunction fun () {/ /} console.log (fun instanceof Object) / / trueconsole.log (fun instanceof Function) / / trueconsole.log ([] instanceof Object) / / trueconsole.log ([] instanceof Array) / / true3, scope
The scope of a variable is called scope or execution context. A variable is no longer visible outside the scope, and all systems have a scope chain when looking for a variable. First from the reference to the variable of the nearest block scope, did not find to continue to look outside the local scope, and then look in the global scope. If you haven't found it yet, you will get the wrong ReferenceError: xxx is not defined.
After reading the above, have you mastered how to understand variables and scopes in JavaScript? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.