const obj1 = { a: 1, b: 2, title: { name: '張三' } }; const obj2 = Object.assign({}, obj1); console.log(obj2); //{ a: 1, b: 2 ,title:{name:'張三'}} obj1.title.name = '李四'; //修改obj1的值,再來打印看下obj2的值 console.log(obj2) //{ a: 1, b: 2,title:{name:'李四'} } console.log(obj1) //{ a: 1, b: 2,title:{name:'李四'} }
var cloneObj = JSON.parse(JSON.stringify(obj));
let freeze1 = { a: '1' }; let freeze2 = Object.freeze(freeze1); freeze2.a = 2; console.log(freeze2) //1
console.log(Object.isFrozen(freeze2)) //true
let seal = {}; Object.seal(seal); //封閉對象
console.log(Object.isSealed({}), '------'); //false console.log(Object.isSealed(seal), '------'); //true
for ...in Object.keys() JSON.stringify()
先看一個例子,建立一個對象javascript
function Person() { this.name = 'KEY' } Person.prototype = { constructr: Person, jos: 'student' }; var key = new Person(); Object.defineProperty(key, 'sex', { value: 'female' }) console.log(key.sex, '------') //female ------ 直接訪問能夠獲取的key.sex for (var pro in key) { //遍歷沒法打印不可枚舉對象 console.log('pro:' + pro + ';' + key[pro]) } pro:name;KEY pro:constructr;function Person(){ this.name='KEY' } pro:jos;student
console.log(Number.propertyIsEnumerable('parseint')) //false
Object.defineProperties(obj, props) *obj 爲源對象 *props是一個對象,裏面有configurable,enumerable,value,writable,get,set *configurable 屬性描述符的類型能夠被改變而且該屬性能夠從對應對象中刪除。默認false *enumerable 是否爲枚舉屬性 默認爲false *value 任意值,默認undefined *writable 能夠經過賦值改變 默認false *get 返回value值,默認undefinde *set 設置value值,默認undefine var defind = {}; Object.defineProperties(defind, { 'v1': { value: '12' } }); console.log(defind.v1) //12
Object.defineProperty(obj,name, prop) *obj 爲源對象 name爲修改的屬性名或設置,prop同上 Object.defineProperty(defind, "key", { enumerable: false, configurable: false, writable: false, value: "static" });
console.log(Object.getOwnPropertyDescriptor(freeze1, 'a')); { value: '1', writable: false, enumerable: true, configurable: false }
console.log(Object.is({}, {})); //false console.log(Object.is([], {})); //false console.log(Object.is([], [])); //false
var o = { prop: 'exists' }; console.log(o.hasOwnProperty('prop')); // 返回 true console.log(o.hasOwnProperty('hasOwnProperty')); // 返回 false
天天進步一點,但願你們喜歡,也但願喜歡的朋友點個贊,後續繼續更新...java