�������ӣ�https://blog.csdn.net/weixin_42752574/article/details/102892378
JS����������/�жϷ���/ջ���/��dz����
һ����������
1�����ֻ�����������
- undefined
- null
- string
- number(ע��NaN(not a number))
- boolean
- symbol(ES6)
2��һ����������
- Object(����Array��Function)
3����ⷽ��
typeof x || typeof (x)
�������:undefined��string��number��boolean��object��function��symbol
����������������Array
let a
console.log(typeof a) // “undefined”
console.log(typeof ‘hello wolrd’) // “string”
console.log(typeof 1024) // “number”
console.log(typeof true) // “boolean”
console.log(typeof []) // “object”
console.log(typeof {}) // “object”
let add = (x, y) => x + y
console.log(typeof add) // “function”
let sym = Symbol()
console.log(typeof sym) // “symbol”
x instanceof type
���������������
�Ƿ���Array || Function || Object(��ԭ������һ��һ�����
)
������������// ע�� Array Function Ҳ��Object
console.log(Array instanceof Object) // true
console.log(Function instanceof Object) // trueconsole.log(2 instanceof Number) // false
console.log(true instanceof Boolean) // false
console.log(‘str’ instanceof String) // falseconsole.log([] instanceof Array) // true
console.log([] instanceof Object) // true
console.log((function(){}) instanceof Function) // true
console.log((function(){}) instanceof Object) // true
console.log(({}) instanceof Object) // true
object.constructor
constructor ���Է��ضԴ����˶�������麯�������á�console.log((2).constructor === Number) //true
console.log(true.constructor === Boolean) //true
console.log((‘str’).constructor === String) //true
console.log(([]).constructor === Array) //true
console.log((function() {}).constructor === Function) //true
console.log(({}).constructor === Object) //true
ps:
(object).constructor,object�������ſ϶������,������ʱ��ᱨ��
��������Ķ��������ԭ�ͣ����������������
function Fn(){} //ԭ���Ƿ���
let f=new Fn()
Fn.prototype=new Array() //�ı�ԭ��Ϊ����
let f1 = new Fn()
console.log(f.constructor===Fn) // true
console.log(f.constructor===Array) // false
console.log(f1.constructor===Fn) // false
console.log(f1.constructor===Array) // true
�������䷽��
// null��ⷽʽ
console.log(null === null) // true ������������
// Array ������ Array.isArray() ���
console.log(Array.isArray([])) // true
����ͷ���:Object.prototype.toString.call()
�ܼ���������ͣ����� “[object type]“�ַ���, ����type�Ƕ�������var a = Object.prototype.toString
console.log(a.call(2)) // “[object Number]”
console.log(a.call(true)) // “[object Boolean]”
console.log(a.call(‘str’)) //“[object String]”
console.log(a.call([])) // “[object Array]”
console.log(a.call(function(){})) // “[object Function]”
console.log(a.call({})) // “[object Object]”
console.log(a.call(undefined)) //“[object Undefined]”
console.log(a.call(null)) // “[object Null]”
4�� undefined��null����
- ����û���𣬶���ʾ���ޡ�
- ϸ����
undefined
��ʾ”ȱ��ֵ
“�������˴�Ӧ����һ��ֵ
�����ǻ�û�ж��塣�����÷��ǣ�
- �����������ˣ���û�и�ֵʱ���͵���undefined��
- ���ú���ʱ��Ӧ���ṩ�IJ���û���ṩ���ò�������undefined��
- ����û�и�ֵ�����ԣ������Ե�ֵΪundefined��
- ����û�з���ֵʱ��Ĭ�Ϸ���undefined��
null
��ʾ”û�ж���
“�����ô���Ӧ����ֵ
�������÷��ǣ�
- ��Ϊ�����IJ�������ʾ�ú����IJ������Ƕ���
- ��Ϊ����ԭ�������յ㡣
����ջ�Ͷ�
1������
- ջ(stack):�Զ�������ڴ�ռ�,����ϵͳ�Զ��ͷ�
- ��(heap)��̬������ڴ�ռ�,��С����,Ҳ�����Զ��ͷ�
2������
- �����������ʹ����ջ��, = ֱ�Ӵ�ֵ
- �����������ʹ��ڷŶ���, = ����ַ
����dz/���
1����������
dz�����������Ҫ������������
������������
let a = 2
let b = a
a = 1
console.log(a, b) // 1 ,2 ����a,bָ��ջ�ﲻͬ����
������������
let a = {c: 2}
let b = a
a.c = 1
console.log(a.c,b.c) //1��1 ���� a,bָ�����ͬ������
Ϊ���ж���������a��b����ϵ
������������Ҫdz/���
- dz����: һ�㿽��
- ���: ���㿽��
2������/����
�����dz��������������������Ͷ���dz������
//1������ =
var arr1 = [1, 2, 3]
var arr2 = arr1
arr1[0]=100
console.log(arr1,arr2) // [ 100, 2, 3 ] [ 100, 2, 3 ]//2��slice
var arr3 = [1, 2, 3]
var arr4 = arr3.slice(-1) // ȡ�������һ��Ԫ��
arr3[2] = 100
console.log(arr3,arr4) // [ 1, 2, 100 ] [ 3 ]
//�������ľ����鲻�ı������飬���������
//���ǣ�����
var arr5 = [1, 2, 3, {b: 4}]
var arr6 = arr5.slice(-1)
arr5[3].b = 100
console.log(arr5, arr6) //[ 1, 2, 3, { b: 100 } ] [ { b: 100 } ]
// ���������Ԫ���Ǹ��������ͣ���ô�����������Ԫ�ر��ı䣬��Ӱ��������
// ����slice()������dz����//3��concat ͬ����
//4������
var arr7 = [1,2,3,{b:4}]
var arr8 = []
for (var i = 0; i < arr7.length; i ++) {
arr8.push(arr7[i])
}
arr7[3].b = 100
console.log(arr7, arr8) // [ 1, 2, 3, { b: 100 } ] [ 1, 2, 3, { b: 100 } ]
�����dz����
// 1�� ����dz���� - Object.assign
function shallowCopy4(origin) {return Object.assign({},origin)
}
//2�� ����dz���� - ��չ�����
// ��չ�������…������ȡ��������������пɱ������ԣ���������ǰ����֮��
function shallowCopy5(origin) {return { ...origin }
}
- ���
1.������JSON�������л�
function deepClone1(origin) {
return JSON.parse(JSON.stringify(origin))
}
- ԭ�������� JSON.stringify ��js�������л���JSON�ַ���������ʹ��JSON.parse�������л�����ԭ��js����
- ȱ�㣺ȱ�����������
undefined��function��symbol
�������������ֵ����������null
2���ݹ�ʵ��
function DeepClone(originObj) {
// ���ж�obj�����黹�Ƕ���
let newObj;
if(originObj instanceof Array ) {
newObj = []
} else if (originObj instanceof Object) {
newObj = {}
}
for (let key in originObj) {
// �ж���Ԫ������
if (typeof(originObj[key]) === "object") {
// �����Ԫ��Ϊobject �ݹ�һ��
newObj[key] = DeepClone(originObj[key])
} else {
newObj[key] = originObj[key]
}
}
return newObj
}