Home > Web Front-end > JS Tutorial > Introduction to JavaScript basic data types, value types and reference types

Introduction to JavaScript basic data types, value types and reference types

巴扎黑
Release: 2017-08-17 14:35:54
Original
1534 people have browsed it

There are four basic data types in JavaScript: numerical values ​​(integers and real numbers), string types (characters or numerical values ​​enclosed by "" or '"), and Boolean types (representing True or False ) and null value. Data in the basic types of JavaScript can be constants or variables. Since JavaScript adopts a weak type, a data variable or constant does not need to be declared first, but is determined when using or assigning a value. The type of data. Of course, you can also declare the type of data first, which automatically indicates the data type when assigning a value.

Variables in JavaScript

Variables in JavaScript are just placeholders, as mentioned in the preface, because of loose types

Define variables:

var name;

The above code defines a variable named name, which can be used to save any value (an uninitialized variable like this will save a special value--undefined), or the value of the variable can be initialized,

var name =”jwy”; //javascript 中的单引号和双引号没有区别,不过要注意正确嵌套name=10;
Copy after login

At first the name was initialized to the string value "jwy", and then changed to the numeric value -100. This approach is not recommended, but it is effective in ECMAScript, and it happens like this in many cases. Do.

You can also define multiple variables in one statement, and many JavaScript frameworks do this.

var name=”jwy”,author,age=29;
Copy after login


It is a good programming habit to add semicolons to each statement. . However, when there are multiple variables, it is recommended to wrap and indent, which can improve readability.

Simple data types

## There are 5 simple data types in ECMAScript. : Undefined, Null, Boolea, Number and String. There is also a complex data type - Object

typeof operator

Since variables in Javascript are loosely typed, So it provides a way to detect the data type of the current variable, that is, the typeof keyword. Among the 5 simple data types in ECMAScript mentioned above, (remember, these 5 are just data types, representing a The data type is the same as the int and string types in C#). Through the typeof keyword, the following values ​​will be returned for these five data types (displayed in string form

"undefined" ---- ------ If the value is undefined Undefined

"boolean" ---------- If the value is a Boolean value Boolean

"string" ---- ------ If the value is a string String

"number" ---------- If the value is a numeric type Number

"object" -- -------- If this value is an object or null Object

"function" ---------- If this value is a function Function

But strictly speaking Say, functions are objects in ECMAScript, and each function is an instance of the Function class. Since a function is an object, it is also a reference type, so a function is just a variable name. Therefore, on many occasions, It is often seen that the function name is passed into the function as a parameter and then called. This is similar to a delegate in C#. Functions will be explained in detail later. After all, many things in Javascript rely on functions. .

Undefined type

As mentioned above, the 5 types are data types, so the data types have values. The value of Undefined is undefined. Note that u is lowercase. If a variable is declared but not initialized, the current value of the variable is undefined. However, it is generally recommended to initialize variables as much as possible, but in early Javascript versions, the value of undefined was not specified, so in some frameworks, in order to be compatible with older browsers, the undefined value will be added to the window object, as follows:

window["undefined"] = window["undefined"]; 
//或者window.undefined = window.undefined;
Copy after login



To put it simply, assign undefined to the undefined attribute of the window object. In older browser objects, there is no undefined attribute, so if the undefined operation is used, it will cause an abnormality, so this is used method, but it will be a bit difficult to understand at first. In older browsers, an undefined value will be returned because there is no window.undefined object, so this can be compatible with old browsers.

However, variables containing undefined values ​​are different from undefined variables, such as:

var name;
alert(name);//undefinedalert(age);// 错误:age is not defined
Copy after login


还没声明过的变量只能执行一项操作,其他全都不能做,就是使用typeof操作符检测其数据类型。

如果不管声明过未初始化以及未声明过的变量执行typeof都是返回undefined的值。两种变来那个都不能执行真正的操作

Null类型

Null类型的值是null,它表示一个空对象指针,没有指向任何对象,如果一个变量的值是null,那当前变量很有可能就是垃圾收集的对象,使用typeof监测null值时会返回”object”,

var person = null;alert(typeof person);//”object”
Copy after login

建议:如果变量是要用来保存对象的额,则初始化为null,这样到时就可以检测该变量是否已经保存了一个对象的引用的,

注意:undefined值是派生自null的,所以对他们执行相等测试会返回true,如:

alert(null == undefind);//true
Copy after login


尽管如此,但是他们用途完全不同,无论在什么情况下都没必要将一个变量的值显示设为undefined,但是这个规则对null却不适用。

Boolean类型

这个类型只有两个值:true 和false。虽然只有两个值,但是javascript中所有类型的值都有与这两个值等价的值。要将一个值转换为对应的Boolean值,可以调用转型函数Boolean() (其实Boolean ,Object,String,Number,Function等这些都是一个函数,构造函数,同样也可以理解为一个类,用类型调用toString()方法会返回这样的东西:

"function Function() { [native code] }" ,Function会相应的改成各自的那个调用函数 )

其实在 if 语句判断中,会对里面的条件自动执行Boolean变化的。

Number类型

数值类型有很多值,最基本的当然就是十进制啦,如:

var num=510;

除了十进制,整数还可以通过八进制或十六进制,其中八进制字面值第一位必须是0,然后是八进制数字序列,如果字面值中的数值超出了范围,那么前导零将被忽略。后面的额数值将被当做十进制数解析。

var num1=070;//八进制的56var num2 =079;//无效的八进制—解析为79var num3 =08;// 无效的八进制—解析为8

而十六进制前面则必须是0x,后跟十六进制数字(0~F),不分大小写。如:

var num1 = 0xA;var num2 = 0x1f;
Copy after login

虽然可以表示为八进制和十六进制,但是计算时会被转换成十进制值。

除了整数,还有浮点数值,当然了,没有像其他强类型语言中的float之类的关键字了。

var num1 = 1.1;
var num2 =0.1;var num3 = .1;//有效,但不推荐
Copy after login

在保存整数时内存分配大小只有浮点数的1/2,所以当浮点数可以转换为整数时,javascript会自动转换为整数。

当然了,除了这些数值比较小的,还有一些极大或极小的数值,可以用科学计数法表示,

var num=123.456e10;

浮点数值的最高精度是17位小数,但是计算时其精确度远远不如整数。例如 ,0.1+0.2不等于0.3,而是0.3000000000000004,所以在做判断时,千万不要用浮点数相加判断等于预想中的某个值。

在javascript中数值最小的是Number.MIN_VALUE,这里可以想象成Number是一个类,而MIN_VALUE 是一个静态变量,储存最小值,同样,最大的是Number.MAX_VALUE。

如果计算中超出了这个最大值和最小值范围,则将被自动转换成Infinity值,如果是负数,就是-Infinity,整数就是Infinity,Infinity的意思是无穷,也就是正负无穷,跟数学中的概念是一样的。但是Infinity是无法参与计算的。可以用原生函数确定是不是有穷:isFinite();只有位于数值范围内才会返回true。

在Javascript中数值除了那些普通的整数、浮点数、最大值、最小值、无穷之外呢,还有一个特殊的值,就是NaN。这个数值用于表示一个本来要返回数值的操作数未返回数值的情况。比如,在C#中任何数值除以0都会报错,抛出异常,但是在Javascript中,任何数值除以0会返回Nan,因此不会影响代码的执行。

NaN的特点:

1、任何设计NaN的操作(如:NaN/0)都会返回NaN.

2、NaN与任何值都不相等,包括NaN本身。如:

alert(NaN == NaN);//false
Copy after login


所以Javascript中有一个isNaN()函数,这个函数接收一个参数,任意类型,它会帮我们确定这个参数是否”不是数值”。它会先尝试先讲这个值转为数值。如果不能被转换为数值的值在调用这个函数之后会返回true,即is NaN 非数值。

至于数值转换,这个内容在Javascript中扩展开来又是一篇文章,有时间再整理整理。

String类型

字符串可以由单引号或双引号表示,在Javascript中这两种引号是等价的,如:

var name = ‘jwy";var author = “jwy”;
Copy after login

不过就是要注意正确嵌套。

字符串可以直接用字面量赋值。任何字符串的长度都可以通过访问气length属性获得。

在Javascript中的字符串是不可变的,其实这跟C#中是一样的,(估计也是为了提高性能),字符串一旦创建,他们的值就不能改变,要改变某个变量保存的字符串,首先要销毁原来的字符串,然后再用另一个包含信纸的字符串填充该变量。

var name=”jwy”;name = “jwy”+” study javascript”;
Copy after login

这里一开始name是保存字符串”jwy”的,第二行代码则将“jwy”+” study javascript”; 值重新赋给name,它先床架一个能容纳这个长度的新字符串,然后填充,销毁原来的字符串。

几乎每个值都有自己的toString()方法,在后面的文章会解释这个方法哪里来的,它会返回相应值的字符串表现。

var age=11;var ageToString =age.toString();//”11”
Copy after login

数值、布尔值、对象和字符串值都有toString(),但是null和undefined值没有这个方法。

一般来说,调用toString()方法不必传递参数,但是,在调用数值的toString方法时,可以传递一个参数,用来指定要输出的数值的基数(看是要输出十进制、二进制、八进制、十六进制表示)

由于null和undefined没有toString方法,在不知道是否是这两者的情况下调用是会报错的,所以还有一条路可以选,就是使用转型函数String(),它呢就可以接收任何类型的值转为字符串了,处理规则如下:

1、如果这个值有toString则直接调用,并返回结果

2、如果是null,则返回”null”

3、如果是unde,则返回”undefined”

Object 类型

Object类型是Javascript引用类型的鼻祖了,(就跟在C#和Java中是一样的道理),在创建Object类型的实例后可以为其添加属性和方法,

var o = new Object;//有效,不推荐var o =new Object();
Copy after login

在Javascript中,Object类型所具有的任何属性和方法也同样存在于更具体的对象中。

每个实例都有如下的属性和方法,如下:

1、constructor ,保存着用于创建当前对象的函数。上面构造函数就是Object();

2、hasOwnProperty,用于检查给定的属性是否在当前对象实例中,是就true,不是在实例中,而是在原型中,则是false;

3、isPrototypeOf,用于检查传入的对象是否是另一个对象的原型

4、propertyIsEnumerable,用于检查给定的属性是否能够使用for…in语句来枚举,

5、toString,返回对象的字符串表示

6、valueOf,返回对象的字符串、数值或布尔值表示,通常与toString方法返回值相同

以上就是Object所具有的属性和方法,所有对象都会因为继承关系而继承这些属性和方法。

结束语

Undefined、Null、Boolean、Number、String是javascript中的基本数据类型,而Object属于引用类型。用typeof检测其他类型会返回相应的字符串,但是检测null或对象时,会返回”object",如果掌握了这些,以后就不会看到这些会点摸不着头脑了。顺带提一句:

Boolean、Number、String 这三个是Javascript中的基本包装类型,也就是这三个其实是一个构造函数,他们是Function的实例,是引用类型,至于这里的String与文章说的String是同名,是因为其实上文说的String是指字符串,这里的String指的是String这个构造函数,上面那么写,是为了更好的理解,因为Javascript是松散类型的。我们可以看下String实例化的例子:

var name = String("jwy");
alert(typeof name);//"object"
var author = "Tom";alert(typeof name);//"string"
Copy after login

The above is the detailed content of Introduction to JavaScript basic data types, value types and reference types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template