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; // 1
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; // 1
copy.a = 2;
copy.a; // 2

// 复制
var o1 = {a: 1};
var o2 = {b: 2};
var copy = Object.assign({}, o1, o2);
copy.a; // 1
copy.b; // 2

// 浅复制
var o = {a: {b: 1}};
var copy = Object.assign({}, o);
copy.a.b; // 1
copy.a.b = 2;
o.a.b; // 2

// 只复制 可枚举的
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; // 3

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; // 2

Object.defineProperty(obj, ‘ohai‘, {
  value: 17
}); // Uncaught TypeError: Cannot define property:ohai, object is not extensible.

Object.defineProperty(obj, ‘a‘, {
  value: 17
});
o.a; // 17

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; // 1
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

MDN Object

时间: 2024-08-11 18:55:01

ES5 方法学习的相关文章

蒙特卡洛方法学习(二)

之前介绍了蒙特卡洛的优势.详情可参考之前的<蒙特卡洛方法学习(一)>. 那么对于我们设计的电路,对于电路中的元器件参数容差,进行统计分布,用一组伪随机数求得元器件的随机抽样序列,对这些随机抽样得到的元器件参数再对设计的电路进行功能仿真,比如:直流分析,交流分析,瞬态分析等等. 利用Multisim进行蒙特卡洛仿真电路.这里举一个简单的例子,如下:   对于上述电路中,电阻的阻值会有一个容差范围,电容的容值也会有一个容差范围,这些容差会对整个电路带来的影响,可以利用蒙特卡洛方法进行仿真,具体操作

os.wark() 方法学习

脚本: #!/usr/bin/env python # encoding utf-8 import os import types Driver = "d:" # 输出D盘下的所有文件 for  i  in os.walk(Driver + os.sep + "python-study"): print (type(i)) print (i) 输出结果 <class 'tuple'> ('d:\\python-study', [], ['1.txt',

C#中的扩展方法学习总结

??版权声明:本文由秦元培创作和发表,采用署名(BY)-非商业性使用(NC)-相同方式共享(SA)国际许可协议进行许可,转载请注明作者及出处,本文作者为秦元培,本文标题为C#中的扩展方法学习总结,本文链接为http://qinyuanpei.com/2015/12/05/extend-methods-of-csharp/. ??各位朋友大家好,我是秦元培,欢迎大家关注我的博客.最近偶然接触到了C#中的扩展方法,觉得这个语法特性是一个不错的特性,因此决定在这里系统地对C#中的扩展方法相关内容进行下

[ExtJS学习笔记]第七节 Extjs的组件components及其模板事件方法学习

本文地址:http://blog.csdn.net/sushengmiyan/article/details/38487519 本文作者:sushengmiyan -------------------------------------------------------------资源链接----------------------------------------------------------------------- 翻译来源  Sencha Cmd官方网站: http://ww

JavaScript面向对象-静态方法-私有方法-公有方法-特权方法,学习

var baseClass= function(){ function show(){//私有方法 alert("访问私有方法"); } function showName(){ alert(this.name); } this.showw = function (){//特权方法 showName(); } } //静态方法 baseClass.showStatic = function(){ alert("访问静态方法"); } //公有方法 baseClass

JavaScript ES6 数组新方法 学习随笔

JavaScript ES6 数组新方法 学习随笔 新建数组 var arr = [1, 2, 2, 3, 4] includes 方法 includes 查找数组有无该参数 有返回true var includes = arr.includes(2) map方法 map 遍历处理返回新数组 原数组不会改变 var map = arr.map(function (item) { return ++item }) reduce方法 reduce 遍历处理数组返回结果 prev与next中间的符号以

JSON.parse()和JSON.stringify()方法学习

JSON.parse()和JSON.stringify()方法学习 JSON对象中有两个非常好用的方法 JSON.parse接受json字符串转化为JS对象 JSON.stringify接收一个JS对象转化为json字符串 JSON对象与普通对象的区别是JSON对象键值对都必须用双引号 例如: const my={ name:"dylan", age:"18" } let myObj=JSON.stringify(my) console.log(myObj)//&q

zepto.1.1.6.js源码中的each方法学习笔记

each方法接受要遍历的对象和对应的回调函数作为参数,它的作用是: 1.如果要遍历的对象是类似数组的形式(以该对象的length属性值的类型是否为number类型来判断),那么就把以要遍历的对象为执行环境,将回调函数放到该执行环境中去循环执行length次: 2.如果要遍历的对象不类似数组,那么用for key in obj 的方法循环执行回调函数key次,同样以要遍历的对象为执行环境,将回调函数放到该执行环境中去循环执行. function each(elements, callback){

clear 方法学习与分析(清除float的简单几个用法)

自己常用的清除浮动方法以分析 设置了float属性之后,它将本来占据一行的元素,可以并列排在一行里.设置浮动属性,可以向左侧或右侧,浮动的框就因此向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止.由于这样,在设置float时,会造成很多不想要的效果,比如父元素高度不存在了,布局混乱了,因此就需要解决这种情况. 我通常使用两中方法: (1)clear属性分析: 添加额外标签<div style="clear:both"></div>在设置浮动文章里