Number
String
Boolean
Undefined
Null
Symbol (ES6 新增,表示獨一無二的值)
1. 基本數據類型var name = 'javascript';
name.toUpperCase(); // 'JAVASCRIPT'
console.log(name); // 'javascript'
let a = 1
console.log(++a) // '2'
let a = { age : 20 };
a.age = 21;
console.log(a.age) //21
let a = 1;
let b = a;
b++
console.log(a) // 1
console.log(b) // 2
let a={id=1234}
let b=a
b.id=1222
console.log(a.id) // 1222
console.log(b.id) // 1222
typeof Symbol();
// symbol 正確
typeof '';
// string 正確
typeof 1;
// number正確
typeof true;
//boolean 正確
typeof undefined;
//undefined 正確
typeof new Function();
// function 正確
typeof null;
//object 無效
typeof [] ;
//object 無效
typeof new Date();
//object 無效
typeof new RegExp();
//object 無效
// 定義構造函數
function C(){}
function D(){}
var o = new C();
o instanceof C; // true,因為 Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false,因為 D.prototype 不在 o 的原型鏈上
o instanceof Object; // true,因為 Object.prototype.isPrototypeOf(o) 返回 true
C.prototype instanceof Object // true,同上
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false,C.prototype 指向了一個空對象,這個空對象不在 o 的原型鏈上.
D.prototype = new C(); // 繼承
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true 因為 C.prototype 現在在 o3 的原型鏈上
[] instanceof Array; //true
{} instanceof Object;//true
new Date() instanceof Date;//true
new RegExp() instanceof RegExp//true
var arr = [1, 2, 3];
console.log(arr instanceof Array) // true
console.log(arr instanceof Object); // true
function fn(){}
console.log(fn instanceof Function)// true
console.log(fn instanceof Object)// true
A instanceof A // true
A instanceof B // true
A instanceof Object true
console.log(1 instanceof Number)//false
console.log(new Number(1) instanceof Number)//true
var aa=[1,2];
console.log(aa.constructor===Array);//true
console.log(aa.constructor===RegExp);//false
console.log((1).constructor===Number);//true
var reg=/^$/;
console.log(reg.constructor===RegExp);//true
console.log(reg.constructor===Object);//false
function Fn(){}
Fn.prototype = new Array()
var f = new Fn
console.log(f.constructor)//Array
Object.prototype.toString.call('') ; // [object String]
Object.prototype.toString.call(1) ; // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window是全局對象global的引用