ES5 方法学习
Object
1. Object.getPrototypeOf(o)
获取对象的prototype对象。等价于以前的o.__proto__
var o = {};
Object.getPrototypeOf(o) === o.__proto__; // true
2. Object.getOwnPropertyNames(o)
获取自有属性名列表。结果列表将不包含原型链上的属性。
var o = { bar: 42, a: 2, b: 3};
Object.getOwnPropertyNames(o); // ["bar", "a", "b"]
var o = {}
o.__proto__.b = 2;
Object.getPrototypeOf(o).c = 3; // 和上面的一句等价
Object.getOwnPropertyNames(o); // []
3. Object.keys
返回对象o的所有可枚举(enumerable)属性的名称。
和 Object.getOwnPropertyNames 区别如下:
var o = {};
// 属性 b 不可枚举
Object.defineProperty(o, 'b', {
value: 1
});
o.b; //
Object.keys(o); // []
Object.getOwnPropertyNames(o); // ["b"]
4. Object.create(proto[, propertiesObject])
The Object.create() method creates a new object with the specified prototype object and properties.
第1个参数是该对象的 prototype, 第2个参数和 Object.defineProperties 第2个参数类似
详见链接;
5. Object.assign
有点像 $.extend 方法, 但是是浅复制
// 复制
var o = {a: 1};
var copy = Object.assign({}, o);
copy.a; //
copy.a = 2;
copy.a; // // 复制
var o1 = {a: 1};
var o2 = {b: 2};
var copy = Object.assign({}, o1, o2);
copy.a; //
copy.b; // // 浅复制
var o = {a: {b: 1}};
var copy = Object.assign({}, o);
copy.a.b; //
copy.a.b = 2;
o.a.b; // // 只复制 可枚举的
var obj = Object.create({ foo: 1 }, { // foo is on obj's prototype chain.
bar: {
value: 2 // bar is a non-enumerable property.
},
baz: {
value: 3,
enumerable: true // baz is an own enumerable property.
}
});
var copy = Object.assign({}, obj);
copy.bar; // undefined;
copy.baz; //
5. Object.prototype.isPrototypeOf(v)
检查对象是否是位于给定对象v的原型链上。
var o = {};
var q = Object.create(o);
o.isPrototypeOf(q);
6. Object.defineProperty(obj, prop, descriptor)
obj 对象, prop 属性名, descriptor 属性值和描述
详见链接;
7. Object.defineProperties(obj, props)
根据对象描述props来定义对象o,通常props包含多个属性的定义。
比 Object.defineProperty 更实用, 因为可以定义多个属性
var obj = {};
Object.defineProperties(obj, {
'property1': {
value: true,
writable: true
},
'property2': {
value: 'Hello',
writable: false
}
// etc. etc.
});
8. Object.getOwnPropertyDescriptor(o,p)
获取对象描述
The Object.getOwnPropertyDescriptor() method returns a property descriptor for an own property (that is, one directly present on an object and not in the object's prototype chain) of a given object.
var o = { get foo() { return 17; } };
var d = Object.getOwnPropertyDescriptor(o, 'foo');
// d is {
// configurable: true,
// enumerable: true,
// get: /*the getter function*/,
// set: undefined
// }
var o = { bar: 42 };
var d = Object.getOwnPropertyDescriptor(o, 'bar');
// d is {
// configurable: true,
// enumerable: true,
// value: 42,
// writable: true
// }
9. Object.seal(o)
seal 单词的意思是
n. 印章,海豹
v. 封闭,密封
The Object.seal() method seals an object, preventing new properties from being added to it and marking all existing properties as non-configurable. Values of present properties can still be changed as long as they are writable.
设置以后不可添加新属性和修改已有属性的特性,但是可以修改已有的属性
var o = {a: 1, b:2};
var obj = Object.seal(o);
obj === o; // true, 注意它们是全等的
Object.isSealed(obj); // true
Object.isFrozen(obj); // false
o.c = 222;
o.c; // undefined
o.a = 2;
o.a; //
Object.defineProperty(obj, 'ohai', {
value: 17
}); // Uncaught TypeError: Cannot define property:ohai, object is not extensible.
Object.defineProperty(obj, 'a', {
value: 17
});
o.a; //
Object.defineProperty(obj, 'a', {
writable: true
}); // Uncaught TypeError: Cannot redefine property: a
// 这样不会报错, 因为没有修改
Object.defineProperty(obj, 'a', {
writable: false
});
10. Object.isSealed(o);
判断一个对象是否sealed
var obj = {};
Object.defineProperties(obj, {
'property1': {
configurable: false
}
});
Object.preventExtensions(obj);
Object.isSealed(obj); // true
11. Object.freeze(o)
和 Object.seal 限制一样,并且还不能修改原来的属性
var o = {a: 1, b:2};
var obj = Object.freeze(o);
obj === o; // true, 注意它们是全等的
Object.isSealed(obj); // true
Object.isFrozen(obj); // true
obj.a = 22;
obj.a; //
obj.c = 22;
obj.c; // undefined;
Object.isFrozen(obj); // true
Object.defineProperty(obj, 'a', {
writable: true
}); // Uncaught TypeError: Cannot redefine property: a
12. Object.isFrozen(o)
判断一个对象是否 frozen
var obj = {};
Object.defineProperties(obj, {
'property1': {
configurable: false,
writable: false
}
});
Object.preventExtensions(obj);
Object.isFrozen(obj); // true
13. Object.preventExtensions(o)
将对象置为不可扩展。
var obj = {};
var o = Object.preventExtensions(obj);
o === obj;
o.a = 1;
o.a; // undefined
Object.isExtensible(o); // true
14. Object.isExtensible(o)
判断一个对象是否可扩展, 默认为 false
15. Object.prototype.propertyIsEnumerable(p)
检查一个对象上的属性p是否可枚举。
var o = {}
Object.defineProperties(o, {
a: {
enumerable: false
},
b: {
enumerable: true
}
});
o.propertyIsEnumerable('a'); // false
o.propertyIsEnumerable('b'); // true
16. Object.getOwnPropertySymbols
待描述;
17. Object.is
判断2个值是否相等
NaN == NaN; // false
NaN === NaN; // false
Object.is(NaN, NaN); // true // Special Cases
Object.is(0, -0); // false
Object.is(-0, -0); // true
Object.is(NaN, 0/0); // true
Array
String
1. String.prototpye.trim
去掉字符串两头的空白符和换行符。
2. 字符订阅
//property access on strings
"abc"[1] === "b"; // 相当于 "abc".charAt(1)
Function
Function.prototype.bind(thisTarget, arg1,…argn)
JSON
JSON.parse(text)
JSON.stringify(obj)
Date
1. Date.now
获取当前时间距1970.1.1 00:00:00的毫秒数。
Date.now(); //1492351123908
2. Date.prototype.toISOString
根据ISO860123生成时间字符串。
(new Date).toISOString(); // "2017-04-16T09:01:23.366Z"
参考链接:
http://pij.robinqu.me/JavaScript_Core/ECMAScript/es5.html
ES5 方法学习的更多相关文章
- 单片机和Linux都想学_换个两全的方法学习单片机
本节教你如何学习单片机,如何选择合适的开发板和开发工具. 现在我们知道单片机是要学习的,那么怎么去学习单片机?在上一课我们说不要使用老一套的方法学习,实际上是指的两个问题. 第一:选择什么开发板: 第 ...
- JavaScript ES6 数组新方法 学习随笔
JavaScript ES6 数组新方法 学习随笔 新建数组 var arr = [1, 2, 2, 3, 4] includes 方法 includes 查找数组有无该参数 有返回true var ...
- java方法学习
java方法学习 方法概念 什么是方法 方法就是完成某些事情的过程,如:实现两个数相加,用方法add(数值1,数值2). 1.System.out.print(),System是系统的一个类,out是 ...
- 用简单的方法学习ES6
ES6 简要概览 这里是ES6 简要概览.本文大量参考了ES6特性代码仓库,请允许我感谢其作者@Luke Hoban的卓越贡献,也感谢@Axel Rauschmayer所作的[优秀书籍]//explo ...
- 常见ES5方法
• ES5 JSON扩展JSON.parseJSON.stringify • ES5 Object扩展Object.createObject.keys • Date对象Date.now • ES5 F ...
- JS数组中every(),filter(),forEach(),map(),some()方法学习笔记!
ES5中定义了五种数组的迭代方法:every(),filter(),forEach(),map(),some(). 每个方法都接受两个参数:要在每一项运行的函数(必选)和运行该函数的作用域的对象-影响 ...
- runtime运行机制方法学习
runtime这玩意第一次听说时都不知道是什么,经过了解后才知道它就是oc动态语言的机制,没有它那oc就不能称为动态语言.在之前可能大家对runtime了解都不深,随着编程技能的日益加深和需要,大家开 ...
- javascript Array 方法学习
原生对象Array学习 Array.from() 从类似数组的对象或可迭代的对象返回一个数组 参数列表 arraylike 类似数组的对象或者可以迭代的对象 mapfn(可选) 对对象遍历映 ...
- zepto.1.1.6.js源码中的each方法学习笔记
each方法接受要遍历的对象和对应的回调函数作为参数,它的作用是: 1.如果要遍历的对象是类似数组的形式(以该对象的length属性值的类型是否为number类型来判断),那么就把以要遍历的对象为执行 ...
随机推荐
- IConfigurationSectionHandler 接口
IConfigurationSectionHandler 处理对特定的配置节的访问. 示例代码: public class MyConfig : IConfigurationSectionHandle ...
- MyISAM和InnoDB的行格式ROW_FORMAT
MyISAM行存储 MyISAM有3种行存储格式:fixed / dynamic / compressed: 格式 说明 备注 fixed 只有当表不包含变长字段(varchar/varbina ...
- github: Permission denied (publickey). 问题解决方法
部署服务器过程中想clone自己github中的库,结果出现Permission denied (publickey).的错误,解决方法是添加服务器公钥到github的settings->SSH ...
- Winform GDI+
什么是GDI+ GDI (Graphics Device Interface), 是属于绘图方面的 API (Application Programming Interface). 因为应用程序不能直 ...
- 换行符 \r \n \r\n 在不同系统下的区别
'\r'是回车,前者使光标到行首,(carriage return)'\n'是换行,后者使光标下移一格,(line feed)\r 是回车,return\n 是换行,newline对于换行这个动作,u ...
- Android应用程序App应用上线流程
对于很多初级开发者,可能对app应用上线不太了解,本文跟大家介绍一下怎么上线app应用.上线App并不是一件很困难的事情,App的应用功能也不需要很强大,甚至不用联网,只有简单的一两个页面的App应用 ...
- Python第三方库SnowNLP(Simplified Chinese Text Processing)快速入门与进阶
简介 github地址:https://github.com/isnowfy/snownlp SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的 ...
- FJOI游记(日记向 不定期更新)
emmmm说实话只是突发奇想开个blog记录记录自己的内心想法罢了.. 2017/12/22:刷了一周的计算几何..刷的死去活来..结果还是被D惨了...譬如都不会考,要考我都不会什么的...感觉内心 ...
- go环境变量及build文件
package main /* windows go环境设置: # 参考:https://blog.csdn.net/quicmous/article/details/80360126 GOROOT ...
- 分布式队列Celery
Celery是什么? Celery 是一个由 Python 编写的简单.灵活.可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分布式系统所需的工具. Celery 专注于实时任务处理,支持任务 ...