trim()、Object.keys()、Object.defineProperty()


A string method


1、trim()

Remove the spaces around the string and returns a new string

var str='  你好啊!  ';			//左右各两个空格
console.log(str.length);
console.log(str.trim().length);

Here Insert Picture Description


Second, the object methods



1、Object.keys()


Returns an object array property name

var o={
    uname:'李明',
    age:20,
    gender:'male',
    grade:80
};
console.log(Object.keys(o));

Here Insert Picture Description

2、Object.defineProperty(obj,prop,descriptor)

The method is to add a new attribute to the object or modify the properties of the original
obj ------ an object to be operated
prop attribute to be operated ------
descriptor attribute value ------ need to operate, require {} as objects of writing, there are some values

value: the value of the property, the default is undefined
Writable: value is rewritten, true | false Default is false
Enumerable: whether the target attribute can be enumerated, true | false Default is false
the Configurable: target properties in the properties (value, writable, whether enumerable, configurable) may be modified, true | false default is false

The default value for the property above is newly created using this method, the default value above which always had the attributes are true, following these can be better understood.

var o={
    uname:'李明',
    age:20,
    gender:'male',
    grade:80
};
Object.defineProperty(o,'age',{		//修改原来的属性
    value:30
});
o.age=50;				//下图的第二行的值由30被改为50
Object.defineProperty(o,'job',{		//创建新的属性
    value:'student'			//而且下图的第一行并没有遍历出job这属性
});
o.job='computer';			//下图的第二行的job并没有被修改
delete  o.job;				//而且也没有被删除
console.log(Object.keys(o));
console.log(o);

Here Insert Picture Description
*** The above shows the default values ​​for the property is newly created using this method, the default value above which the properties of the original, there are true rather than false ***

Published 24 original articles · won praise 0 · Views 254

Guess you like

Origin blog.csdn.net/weixin_45969777/article/details/104873289