JavaScript基础——变量类型和计算
q:JS中使用 typeof
的类型?
`undefined` `null` `boolean` `number` `string`
`object`
`对象` `数组` `函数`
typeof undefined; //undefined
typeof 'abc'; //string
typeof 123; //number
typeof true; //boolean
typeof {}; //object
typeof []; //object
typeof null; //object 引用类型
typeof console.log; //function
//typeof 只能区分基本类型,没法区分对象、数组、null这三种引用类型
q:什么时候使用 ===
什么时候使用 ==
//字符串拼接类型转换
var a = 100 + 10 //110
var b = 100 + '10' //10010
// ==号
100 == '100' //true
0 == '' //true
null == undefined //true
//if语句
var a = true;
if (a) {
//
}
var b = 100;
if (b) {
//b=true
}
var c = '';
if (c) {
//c=false
}
//逻辑运算符
console.log(10 && 0) // 0
console.log('' || 'abc') // 'abc'
console.log(!window.abc) // true (当window.abc=undefined时)
// 判断一个变量是被当作 `true` 仍是 `false`
var m = 100;
console.log(!m) //false
console.log(!!m) //true
// a:
if (obj.a == null) {
// 至关于obj.a=== null||obj.a=== undefined,简写形式
// 这是jquery源码中推荐的写法
// 其余状况所有使用 `===`
}
q:JS中有哪些 内置函数
-数据封装类对象
Object
Array
Boolean
Number
String
Function
Date
RegExp
Error
q:JS按照 存储方式
区分为哪些类型,并描述其特色
//值类型
var a = 10;
b = a;
a = 11;
console.log(b) //10
//复制不会相互干预
** ** ** ** ** ** ** ** ** *
//引用类型
var obj1 = { x: 100 };
var obj2 = obj1;
obj1.x = 200;
console.log(obj2.x); //200
// 复制是引用类型的指针,会相互干预
q:如何理解 JSON
// JSON只不过是一个内置的JS对象而已
// JSON也是一种数据格式
JSON.stringify({ a: 100, b: 200 }); // "{"a":100,"b":200}"
JSON.parse('{"a":100,"b":200}'); // {a:100,b:200}