当前位置:网站首页>Insight into heap and stack stored in new string() /string() in JS
Insight into heap and stack stored in new string() /string() in JS
2022-06-25 13:22:00 【wendyTan10】
stay javascript There are two ways to store variables in memory , Value type and Reference type ;
Value type : character string (string)、 The number (number)、 Boolean value (boolean)、undefined、null
(ECMAScript 2016 Added a basic data type :symbol )
The main features are as follows :
- . The footprint is fixed , Save it in the stack
- What is saved and copied is the value itself
- Use
typeofType of detection data
Reference type : object (Object)、 Array (Array)、 function (Function)
- The occupied space is not fixed , Keep it in the pile
- Saving and copying is a pointer to an object
instanceofTest data type- new() Method is a reference type
To explore the String() and new String() The difference of
There are three ways to define a string ;
var csdn1 = 'csdn';
var csdn2 = String('csdn');
var csdn3 = new String('csdn');
And judge whether the values and types of three variables are equal , Here we use === Congruence
console.log(csdn1 === csdn2); //true
console.log(csdn1 === csdn3); //false
console.log(csdn2 === csdn3); //false
Mainly because csdn1 And csdn2 Value type , and csdn3 Is a reference type
console.log(typeof csdn1); // string
console.log(typeof csdn2); // string
console.log(typeof csdn3); //object
Values of basic types can call the corresponding length Method , But I can't define myself ;
console.log(csdn1.length); // 4
csdn1.say = 'hello';
console.log(csdn1.say); // undefined
console.log(csdn3.lengh); // 4
csdn3.say = 'hello';
console.log(csdn3.say); // hello
Because of the csdn1 Value type ( Source material type ) , and csdn3 Is a reference type ( Packaging object ), adopt new String() created string Instance object of , So has the
csdn3 instanceof String // true
but csdn1 And csdn2 Value type , Nature does not belong to String The method in
csdn1 instanceof String // false
csdn2 instanceof String // false
however csdn1,csdn2 But have String Properties and methods in
console.log(csdn1.indexOf === String.prototype.indexOf); // true
console.log(csdn2.indexOf === String.prototype.indexOf); // true
console.log(csdn3.indexOf === String.prototype.indexOf); // true
Value type ( Source material type ) Accessible reference types ( Packaging object ) All methods and properties in , But they don't have
That's why csdn1 And csdn2( Value type ) You can visit length And indexof, Is itself stored on the stack , Therefore, it does not have the characteristics of setting properties and methods ; and csdn3( Reference object ) Is the storage heap pointer , Can be used to add properties and methods ;
Whenever a string is accessed in read mode , Three steps that will be performed in the background :
- Create a String Instance of type
- Call a specific method on the instance
- Consumption instance
Think of these three steps as performing the following 3 That's ok ECMAScript Code for :
let s1 = new String("some text");
let s2 = s1.substring(2);
s1 = null;
This behavior allows the original value to have the behavior of the object ;
additional :
.toString() and String() Can convert data into strings ,undefined and null No, toString() Method , You have to use String() Method returns these two Literal text of the value .
toString() Can receive a base parameter , That is to What is the base number to output the string representation of the value . The default is decimal . By passing in the parameter Count , You can get the binary value 、 octal 、 Hexadecimal , Or anything else String representation of the valid cardinality , such as :
let num = 10; console.log(num.toString()); // 10
console.log(num.toString(2)); // 1010
console.log(num.toString(8)); // 12
console.log(num.toString(10)); // 10
console.log(num.toString(16)); // a
If you are not sure whether a value is null or undefined , You can make use String() Transformation function , It always returns a string representing the value of the corresponding type ;
let value1 = 10;
let value2 = true;
let value3 = null;
let value4;
console.log(String(value1)); // 10
console.log(String(value2)); // true
console.log(String(value3)); // null
console.log(String(value4)); // undefined
边栏推荐
- [pit avoidance refers to "difficult"] antd cascader implements new customized functions
- [data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
- Sword finger offer 04 Find in 2D array
- Sword finger offer day 3 string (simple)
- C# 切换中英文输入法
- Detailed explanation of string operation functions and memory functions
- 剑指 Offer II 028. 展平多级双向链表
- 关于一个图书小系统的实现
- Nova中的api
- Openstack learning notes -nova component insight
猜你喜欢

Detailed explanation of string operation functions and memory functions

药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质

leetcode:456. 132 模式【单调栈】

[pit avoidance means "difficult"] to realize editable drag and drop sorting of protable

Sword finger offer II 025 Adding two numbers in a linked list

Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream

Analyse de l'optimisation de la réécriture des requêtes lazyagg de l'entrepôt

Explanation of a textbook question

中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队

There is a problem with the date when MySQL imports and exports data to excel
随机推荐
剑指 Offer II 025. 链表中的两数相加
À propos du stockage des données en mémoire
The starting point for learning programming.
Lexical trap
Nova组件源码分析之冷迁移与Resize
There is a problem with the date when MySQL imports and exports data to excel
德国举行全球粮食安全团结会议
[pit avoidance refers to "difficult"] halfcheckedkeys rendering problem in semi selected status of antd tree
Seven competencies required by architects
Optimization of lazyagg query rewriting in parsing data warehouse
New Gospel of drug design: Tencent, together with China University of science and technology and Zhejiang University, developed an adaptive graph learning method to predict molecular interactions and
[pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
Summer Ending
关于猜数字游戏的实现
与生产环境中的 console.log 说再见
数据在内存中的存储相关内容
Openstack -- creating virtual machines for Nova source code analysis
AGCO AI frontier promotion (6.25)
MySQL learning notes
.NET in China - What's New in .NET