[关闭]
@wh1100717 2015-01-24T04:18:22.000000Z 字数 15042 阅读 617

数据大学 - ECMAScript5详解

数据大学 ES5


浏览器兼容性:Opera 11.60+、Internet Explorer 9+、Firefox 4+、Safari 5.1+、Chrome 13+

IE9不支持严谨模式,但IE10是支持的

为了保证js代码得到通用性的支持,可以在html中增加如下代码:

  1. <!--[if lt IE 9]>
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.0.5/es5-shim.js"></script>
  3. <![endif]-->

es5-shim提供了ES5的部分实现,其中有一部分需要依赖底层engine,为了保证在地浏览器中js不报错,对其中该部分的API做了封装,但是并不保证一定能正确执行,需要额外引用es5-sham。简单来说,如果需要考虑浏览器兼容性的问题,则不要使用Object在ES5上进行的扩展。具体请参考如下文档 https://github.com/es-shims/es5-shim

Array

Array.isArray(arg)

isArray 函数需要一个参数 arg,如果参数是个对象并且 class 内部属性是 "Array", 返回布尔值 true;否则它返回 false。采用如下步骤:

  1. Array.isArray([1,2,3]) //true
  2. Array.isArray({a:1}) //false

Array.prototype.every(callbackfn [, thisArg])

检查数组的每一个元素是否符合callbackfn的要求。只有遍历数组中的每一个元素都返回true时,最终才会返回true,否则返回false。

  1. var arr1 = [1,2,3,4,5]
  2. var arr2 = [1,2,'3',4,5]
  3. var isNumber = function(item, index){
  4. console.log(typeof item, index);
  5. return typeof item === 'number';
  6. }
  7. //当数组按照索引全部遍历完并且每次callbackfn都返回true,则最终every函数返回true
  8. arr1.every(isNumber)
  9. /*
  10. number 0
  11. number 1
  12. number 2
  13. number 3
  14. number 4
  15. true
  16. */
  17. //当callbackfn返回false,则every函数直接返回false
  18. arr2.every(isNumber)
  19. /*
  20. number 0
  21. number 1
  22. string 2
  23. false
  24. */
  25. var isNumberArg = function(item, index){
  26. console.log(this.x, index);
  27. return typeof item === 'number'
  28. }
  29. //我们可以给every函数添加指定域,可以通过this.attr来引用域中的属性。
  30. arr2.every(isNumberArg, {x:1})
  31. /*
  32. 1 0
  33. 1 1
  34. 1 2
  35. false
  36. */

Array.prototype.some(callbackfn [, thisArg])

与Array.prototype.every相反,只要有任何一个元素返回true,则返回true,否则返回false。
thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,3,4,5]
  2. arr.some(function(item, index){
  3. return item > 4;
  4. })

Array.prototype.filter(callbackfn [, thisArg])

过滤器,返回满足callbackfn的item项(即callbackfn为true的项)。 thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,'3',4,5]
  2. var isNumber = function(item, index){
  3. console.log(typeof item, index);
  4. return typeof item === 'number';
  5. }
  6. arr.filter(isNumber)
  7. /*
  8. number 0
  9. number 1
  10. string 2
  11. number 3
  12. number 4
  13. [1, 2, 4, 5]
  14. */

Array.prototype.forEach(callbackfn [, thisArg])

使用callbackfn来遍历数组,返回undefined, thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,3,4,5]
  2. arr.forEach(function(item, index, array){
  3. console.log(item, index);
  4. })
  5. /*
  6. 1 0
  7. 2 1
  8. 3 2
  9. 4 3
  10. 5 4
  11. */

Array.prototype.indexOf(searchElement [, fromIndex])

按照数组升序返回数组中元素与searchElement严格相等的元素, fromIndex表示开始遍历查询的元素下标。

  1. var arr = ["hello", "world", "hello", "man", {a: 1}]
  2. arr.indexOf("hello") //0
  3. arr.indexOf("hello", 1) //2
  4. arr.indexOf({a:1}) //-1 因为{a: 1}为两个对象,并不严格相等,因此不会返回其对应的index

Array.prototype.lastIndexOf(searchElement [, fromIndex])

与Array.prototype.indexOf,不过期按照数组降序进行匹配。fromIndex表示开始遍历查询的元素下标。

  1. var arr = ["hello", "world", "hello", "man", {a: 1}]
  2. arr.lastIndexOf("hello") //2
  3. arr.lastIndexOf("hello", 1) //0

Array.prototype.map(callbackfn [, thisArg])

对数组内的元素按照callbackfn进行更改并得到新的数组。thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,3,4,5]
  2. arr2 = arr.map(function(item, index){
  3. return item * item
  4. })
  5. console.log(arr2) //[1, 4, 9, 16, 25]

Array.prototype.reduce(callbackfn [, initialValue])

callbackfn接受4个参数:

initialValue可以指定初始值。

  1. var arr = ["hello", "world", "aaa", "bbb"]
  2. var reduce_str = arr.reduce(function(previousValue, currentValue, currentIndex, array){
  3. return previousValue + "[" + currentValue + "]"
  4. })
  5. console.log(reduce_str) //hello[world][aaa][bbb]
  6. var reduce_str2 = arr.reduce(function(previousValue, currentValue, currentIndex, array){
  7. return previousValue + "[" + currentValue + "]"
  8. }, "[Init Value]")
  9. console.log(reduce_str2) // [Init Value][hello][world][aaa][bbb]

Array.prototype.reduceRight(callbackfn [, initialValue])

与reduce相比,用法类似。实现上差异在于reduceRight是从数组的末尾开始实现。

  1. var arr = ["hello", "world", "aaa", "bbb"]
  2. var reduce_str = arr.reduceRight(function(previousValue, currentValue, currentIndex, array){
  3. return previousValue + "[" + currentValue + "]"
  4. })
  5. console.log(reduce_str) // bbb[aaa][world][hello]
  6. var reduce_str2 = arr.reduceRight(function(previousValue, currentValue, currentIndex, array){
  7. return previousValue + "[" + currentValue + "]"
  8. }, "[Init Value]")
  9. console.log(reduce_str2) // [Init Value][bbb][aaa][world][hello]

Date

Date.now()

now 函数返回一个数字值,它表示调用 now 时的 UTC 日期时间的时间值。等价于(new Date()).getTime()

  1. console.log("Current Time is: ", Date.now())

Date.prototype.toJSON(key)

此函数为 JSON.stringify 提供 Date 对象的一个字符串表示。

  1. var now = new Date(1421762769200)
  2. now.toJSON() //"2015-01-20T14:06:09.200Z"

Date.parse(string)

将字符串转为UTC时间值

  1. Date.parse("2015-01-20T14:06:09.200Z") //1421762769200

Date.prototype.toISOString()

此函数返回一个代表 --this Date 对象表示的时间的实例 -- 的字符串。字符串的格式是 15.9.1.15 定义的日期时间字符串格式。字符串中包含所有的字段。字符串表示的时区总是 UTC,用后缀 Z 标记。如果 this 对象的时间值不是有限的数字值,抛出一个 RangeError 异常。

  1. var now = new Date(1421762769200)
  2. now.toISOString() //"2015-01-20T14:06:09.200Z"

Number && String && Function

parseInt(string, radix)

parseInt 函数根据指定的参数 radix,和 string 参数的内容解释结果来决定,产生一个整数值。
radix表示进制,范围在2和36之间,默认为10进制。

  1. parseInt("1234") //1234
  2. parseInt("10101", 2) //21

Number.prototype.toFixed(fractionDigits)

Number四舍五入,fractionDigits表示保留的位数。fractionDigits范围为0到20

  1. (123.456).toFixed() //123
  2. (123.456).toFixed(1) //123.5
  3. (123.456).toFixed(2) //123.46

String.prototype.split(separator, limit)

将字符串按照separator进行拆分,并返回对应的数组。其中separator可以为字符串或者正则表达式,limit限制返回数组的总长度。

  1. "hello,world,aaa,bbb".split(",") //["hello", "world", "aaa", "bbb"]
  2. "hello,world,aaa,bbb".split(",",2) //["hello", "world"]
  3. "a\nb\r\nc\rd".split(/\r?\n/) //["a", "b", "cd"]

String.prototype.trim()

返回去除字符串头尾空格和换行符的字符串。

  1. " \n t\ne\ts t \n \t".trim()
  2. /*
  3. "t
  4. e s t"
  5. */

String.prototype.replace(searchValue, replaceValue)

将字符串按照sarchValue进行匹配,并按照repalceValue进行替换。searchValue可以为字符串或正则表达式,repalceValue可以为字符串或函数。

  1. var name = 'aaa bbb ccc';
  2. var uw = name.replace(/\b\w+\b/g, function(word){
  3. return word.substring(0,1).toUpperCase()+word.substring(1);}
  4. );
  5. console.log(uw); // Aaa Bbb Ccc

Function.prototype.bind()

bind()方法会创建一个新函数,称为绑定函数.

当调用这个绑定函数时,绑定函数会以创建它时传入 bind()方法的第一个参数作为 this,传入 bind() 方法的第二个以及以后的参数加上绑定函数运行时本身的参数按照顺序作为原函数的参数来调用原函数.

  1. var getX = function(m, n){return this.x + m + n}
  2. var module = {
  3. x: 1
  4. }
  5. var boundGetX = getX.bind(module, 2, 3)
  6. boundGetX() // 6

Object

Object.keys(obj)

以数组的形式返回Obj对象的所有key

  1. var obj = {a:1, b:2, c:3}
  2. Object.keys(obj) //["a", "b", "c"]

注:Object.kyes()之后的的API在不支持的浏览器中会产生错误,引用es5-shim.js并不支持以下API,如果需要保证至少在低版本浏览器中不报错误,需要引用es5-sham.js,其并不能保证js会正确执行,但会禁止其对外暴露错误,请慎重使用。

Object.create(O[, Properties])

以指定的原型创建对象,并且可以(可选)设置对象的属性。

Properties包含 value 特性,以及 writable、enumerable 和 configurable 特性。如果未指定最后三个特性,则它们默认为 false。

返回一个具有指定的内部原型且包含指定的属性(如果有)的新对象。# 数据大学 - ECMAScript 5 详解

标签(空格分隔): 数据大学 ES5


浏览器兼容性:Opera 11.60+、Internet Explorer 9+、Firefox 4+、Safari 5.1+、Chrome 13+

IE9不支持严谨模式,但IE10是支持的

为了保证js代码得到通用性的支持,可以在html中增加如下代码:

  1. <!--[if lt IE 9]>
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.0.5/es5-shim.js"></script>
  3. <![endif]-->

es5-shim提供了ES5的部分实现,其中有一部分需要依赖底层engine,为了保证在地浏览器中js不报错,对其中该部分的API做了封装,但是并不保证一定能正确执行,需要额外引用es5-sham。简单来说,如果需要考虑浏览器兼容性的问题,则不要使用Object在ES5上进行的扩展。具体请参考如下文档 https://github.com/es-shims/es5-shim

Array

Array.isArray(arg)

isArray 函数需要一个参数 arg,如果参数是个对象并且 class 内部属性是 "Array", 返回布尔值 true;否则它返回 false。采用如下步骤:

  1. Array.isArray([1,2,3]) //true
  2. Array.isArray({a:1}) //false

Array.prototype.every(callbackfn [, thisArg])

检查数组的每一个元素是否符合callbackfn的要求。只有遍历数组中的每一个元素都返回true时,最终才会返回true,否则返回false。

  1. var arr1 = [1,2,3,4,5]
  2. var arr2 = [1,2,'3',4,5]
  3. var isNumber = function(item, index){
  4. console.log(typeof item, index);
  5. return typeof item === 'number';
  6. }
  7. //当数组按照索引全部遍历完并且每次callbackfn都返回true,则最终every函数返回true
  8. arr1.every(isNumber)
  9. /*
  10. number 0
  11. number 1
  12. number 2
  13. number 3
  14. number 4
  15. true
  16. */
  17. //当callbackfn返回false,则every函数直接返回false
  18. arr2.every(isNumber)
  19. /*
  20. number 0
  21. number 1
  22. string 2
  23. false
  24. */
  25. var isNumberArg = function(item, index){
  26. console.log(this.x, index);
  27. return typeof item === 'number'
  28. }
  29. //我们可以给every函数添加指定域,可以通过this.attr来引用域中的属性。
  30. arr2.every(isNumberArg, {x:1})
  31. /*
  32. 1 0
  33. 1 1
  34. 1 2
  35. false
  36. */

Array.prototype.some(callbackfn [, thisArg])

与Array.prototype.every相反,只要有任何一个元素返回true,则返回true,否则返回false。
thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,3,4,5]
  2. arr.some(function(item, index){
  3. return item > 4;
  4. })

Array.prototype.filter(callbackfn [, thisArg])

过滤器,返回满足callbackfn的item项(即callbackfn为true的项)。 thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,'3',4,5]
  2. var isNumber = function(item, index){
  3. console.log(typeof item, index);
  4. return typeof item === 'number';
  5. }
  6. arr.filter(isNumber)
  7. /*
  8. number 0
  9. number 1
  10. string 2
  11. number 3
  12. number 4
  13. [1, 2, 4, 5]
  14. */

Array.prototype.forEach(callbackfn [, thisArg])

使用callbackfn来遍历数组,返回undefined, thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,3,4,5]
  2. arr.forEach(function(item, index, array){
  3. console.log(item, index);
  4. })
  5. /*
  6. 1 0
  7. 2 1
  8. 3 2
  9. 4 3
  10. 5 4
  11. */

Array.prototype.indexOf(searchElement [, fromIndex])

按照数组升序返回数组中元素与searchElement严格相等的元素, fromIndex表示开始遍历查询的元素下标。

  1. var arr = ["hello", "world", "hello", "man", {a: 1}]
  2. arr.indexOf("hello") //0
  3. arr.indexOf("hello", 1) //2
  4. arr.indexOf({a:1}) //-1 因为{a: 1}为两个对象,并不严格相等,因此不会返回其对应的index

Array.prototype.lastIndexOf(searchElement [, fromIndex])

与Array.prototype.indexOf,不过期按照数组降序进行匹配。fromIndex表示开始遍历查询的元素下标。

  1. var arr = ["hello", "world", "hello", "man", {a: 1}]
  2. arr.lastIndexOf("hello") //2
  3. arr.lastIndexOf("hello", 1) //0

Array.prototype.map(callbackfn [, thisArg])

对数组内的元素按照callbackfn进行更改并得到新的数组。thisArg与Array.prototype.every中使用方法相同。

  1. var arr = [1,2,3,4,5]
  2. arr2 = arr.map(function(item, index){
  3. return item * item
  4. })
  5. console.log(arr2) //[1, 4, 9, 16, 25]

Array.prototype.reduce(callbackfn [, initialValue])

callbackfn接受4个参数:

initialValue可以指定初始值。

  1. var arr = ["hello", "world", "aaa", "bbb"]
  2. var reduce_str = arr.reduce(function(previousValue, currentValue, currentIndex, array){
  3. return previousValue + "[" + currentValue + "]"
  4. })
  5. console.log(reduce_str) //hello[world][aaa][bbb]
  6. var reduce_str2 = arr.reduce(function(previousValue, currentValue, currentIndex, array){
  7. return previousValue + "[" + currentValue + "]"
  8. }, "[Init Value]")
  9. console.log(reduce_str2) // [Init Value][hello][world][aaa][bbb]

Array.prototype.reduceRight(callbackfn [, initialValue])

与reduce相比,用法类似。实现上差异在于reduceRight是从数组的末尾开始实现。

  1. var arr = ["hello", "world", "aaa", "bbb"]
  2. var reduce_str = arr.reduceRight(function(previousValue, currentValue, currentIndex, array){
  3. return previousValue + "[" + currentValue + "]"
  4. })
  5. console.log(reduce_str) // bbb[aaa][world][hello]
  6. var reduce_str2 = arr.reduceRight(function(previousValue, currentValue, currentIndex, array){
  7. return previousValue + "[" + currentValue + "]"
  8. }, "[Init Value]")
  9. console.log(reduce_str2) // [Init Value][bbb][aaa][world][hello]

Date

Date.now()

now 函数返回一个数字值,它表示调用 now 时的 UTC 日期时间的时间值。等价于(new Date()).getTime()

  1. console.log("Current Time is: ", Date.now())

Date.prototype.toJSON(key)

此函数为 JSON.stringify 提供 Date 对象的一个字符串表示。

  1. var now = new Date(1421762769200)
  2. now.toJSON() //"2015-01-20T14:06:09.200Z"

Date.parse(string)

将字符串转为UTC时间值

  1. Date.parse("2015-01-20T14:06:09.200Z") //1421762769200

Date.prototype.toISOString()

此函数返回一个代表 --this Date 对象表示的时间的实例 -- 的字符串。字符串的格式是 15.9.1.15 定义的日期时间字符串格式。字符串中包含所有的字段。字符串表示的时区总是 UTC,用后缀 Z 标记。如果 this 对象的时间值不是有限的数字值,抛出一个 RangeError 异常。

  1. var now = new Date(1421762769200)
  2. now.toISOString() //"2015-01-20T14:06:09.200Z"

Number && String && Function

parseInt(string, radix)

parseInt 函数根据指定的参数 radix,和 string 参数的内容解释结果来决定,产生一个整数值。
radix表示进制,范围在2和36之间,默认为10进制。

  1. parseInt("1234") //1234
  2. parseInt("10101", 2) //21

Number.prototype.toFixed(fractionDigits)

Number四舍五入,fractionDigits表示保留的位数。fractionDigits范围为0到20

  1. (123.456).toFixed() //123
  2. (123.456).toFixed(1) //123.5
  3. (123.456).toFixed(2) //123.46

String.prototype.split(separator, limit)

将字符串按照separator进行拆分,并返回对应的数组。其中separator可以为字符串或者正则表达式,limit限制返回数组的总长度。

  1. "hello,world,aaa,bbb".split(",") //["hello", "world", "aaa", "bbb"]
  2. "hello,world,aaa,bbb".split(",",2) //["hello", "world"]
  3. "a\nb\r\nc\rd".split(/\r?\n/) //["a", "b", "cd"]

String.prototype.trim()

返回去除字符串头尾空格和换行符的字符串。

  1. " \n t\ne\ts t \n \t".trim()
  2. /*
  3. "t
  4. e s t"
  5. */

String.prototype.replace(searchValue, replaceValue)

将字符串按照sarchValue进行匹配,并按照repalceValue进行替换。searchValue可以为字符串或正则表达式,repalceValue可以为字符串或函数。

  1. var name = 'aaa bbb ccc';
  2. var uw = name.replace(/\b\w+\b/g, function(word){
  3. return word.substring(0,1).toUpperCase()+word.substring(1);}
  4. );
  5. console.log(uw); // Aaa Bbb Ccc

Function.prototype.bind()

bind()方法会创建一个新函数,称为绑定函数.

当调用这个绑定函数时,绑定函数会以创建它时传入 bind()方法的第一个参数作为 this,传入 bind() 方法的第二个以及以后的参数加上绑定函数运行时本身的参数按照顺序作为原函数的参数来调用原函数.

  1. var getX = function(m, n){return this.x + m + n}
  2. var module = {
  3. x: 1
  4. }
  5. var boundGetX = getX.bind(module, 2, 3)
  6. boundGetX() // 6

Object

Object.keys(obj)

以数组的形式返回Obj对象的所有key

  1. var obj = {a:1, b:2, c:3}
  2. Object.keys(obj) //["a", "b", "c"]

注:Object.kyes()之后的的API在不支持的浏览器中会产生错误,引用es5-shim.js并不支持以下API,如果需要保证至少在低版本浏览器中不报错误,需要引用es5-sham.js,其并不能保证js会正确执行,但会禁止其对外暴露错误,请慎重使用。

Object.create(O[, Properties])

以指定的原型创建对象,并且可以(可选)设置对象的属性。

Properties包含 value 特性,以及 writable、enumerable 和 configurable 特性。如果未指定最后三个特性,则它们默认为 false。

返回一个具有指定的内部原型且包含指定的属性(如果有)的新对象。

  1. var obj = Object.create({a:1},{
  2. x: {
  3. value: 1,
  4. writable: true,
  5. enumerable: true,
  6. configurable: true
  7. }
  8. })
  9. console.log(obj) //{x: 1} #注:Chrome控制台会将对象的__proto__中的键值进行输出,而nodejs不会。
  10. console.log(obj.__proto__) //{a:1}

Object.getPrototypeOf(obj)

返回对象的原型

  1. var A = function(){this.x = 1}
  2. A.prototype.y = 2
  3. var a = new A()
  4. console.log(Object.getPrototypeOf(a)) //{y:2}

Object.getOwnPropertyNames(obj)

返回对象自己的属性的名称。一个对象的自己的属性是指直接对该对象定义的属性,而不是从该对象的原型继承的属性。 对象的属性包括字段(对象)和函数。

  1. var A = function(){this.x = 1}
  2. A.prototype.y = 2
  3. var a = new A()
  4. console.log(Object.getOwnPropertyNames(a)) //['x']

Object.seal(obj)

阻止修改现有属性的特性(将每个属性的 configurable 设置为 false),并阻止添加新属性。

  1. var obj = {a: 1, b: 2}
  2. Object.seal(obj)
  3. obj.c = 3
  4. obj.a = 0
  5. console.log(obj) //{a: 0, b: 2}
  6. console.log(obj.c) //undefined

Object.freeze(obj)

阻止修改现有属性的特性和值(将每个属性的 configurable 和 writable 设置为 false),并阻止添加新属性。

  1. var obj = {a: 1, b: 2}
  2. Object.freeze(obj)
  3. obj.c = 3
  4. obj.a = 0
  5. console.log(obj) //{a: 1, b: 2}
  6. console.log(obj.c) //undefined

Object.preventExtensions(obj)

阻止向对象添加新属性。

  1. var obj = {a: 1, b: 2}
  2. Object.preventExtensions(obj)
  3. obj.c = 3
  4. obj.a = 0
  5. console.log(obj) //{a: 0, b: 2}
  6. console.log(obj.c) //undefined

Object.isSealed(obj)

如果无法在对象中修改现有属性的特性,且无法向对象添加新属性,则返回 true。

Object.isFrozen(obj)

如果无法在对象中修改现有属性的特性和值,且无法向对象添加新属性,则返回 true。

Object.isExtensible(obj)

返回一个值,该值指示是否可向对象添加新属性。

Object.getOwnPropertyDescriptor(obj, propertyname)

获取指定对象自己的属性描述符。自己的属性描述符是直接在对象上定义的描述符,而不是从对象的原型继承的描述符。

  1. obj = {a: 1}
  2. var descriptor = Object.getOwnPropertyDescriptor(obj, "a")
  3. console.log(descriptor) //{ value: 1, writable: true, enumerable: true, configurable: true }

Object.defineProperty(obj, propertyname, descriptor)

将属性添加到对象或修改现有属性的特性。

  1. var obj = {}
  2. Object.defineProperty(obj, "newDataProperty", {
  3. value: 101,
  4. writable: true,
  5. enumerable: true,
  6. configurable: true
  7. })
  8. console.log(obj.newDataProperty) //101

Object.defineProperties(obj, descriptors)

将一个或多个属性添加到对象,并/或修改现有属性的特性。

descriptors包含一个或多个描述符对象的 JavaScript 对象。每个描述符对象描述一个数据属性或访问器属性。

  1. var obj = {}
  2. Object.defineProperties(obj, {
  3. newDataProperty: {
  4. value: 101,
  5. writable: true,
  6. enumerable: true,
  7. configurable: true
  8. },
  9. newAccessorProperty: {
  10. set: function (x) {
  11. document.write("in property set accessor" + newLine);
  12. this.newaccpropvalue = x;
  13. },
  14. get: function () {
  15. document.write("in property get accessor" + newLine);
  16. return this.newaccpropvalue;
  17. },
  18. enumerable: true,
  19. configurable: true
  20. }
  21. })
  22. console.log(obj) //{ newDataProperty: 101, newAccessorProperty: [Getter/Setter] }
  1. var obj = Object.create({a:1},{
  2. x: {
  3. value: 1,
  4. writable: true,
  5. enumerable: true,
  6. configurable: true
  7. }
  8. })
  9. console.log(obj) //{x: 1} #注:Chrome控制台会将对象的__proto__中的键值进行输出,而nodejs不会。
  10. console.log(obj.__proto__) //{a:1}

Object.getPrototypeOf(obj)

返回对象的原型

  1. var A = function(){this.x = 1}
  2. A.prototype.y = 2
  3. var a = new A()
  4. console.log(Object.getPrototypeOf(a)) //{y:2}

Object.getOwnPropertyNames(obj)

返回对象自己的属性的名称。一个对象的自己的属性是指直接对该对象定义的属性,而不是从该对象的原型继承的属性。 对象的属性包括字段(对象)和函数。

  1. var A = function(){this.x = 1}
  2. A.prototype.y = 2
  3. var a = new A()
  4. console.log(Object.getOwnPropertyNames(a)) //['x']

Object.isSealed(obj)

如果无法在对象中修改现有属性的特性,且无法向对象添加新属性,则返回 true。

Object.isFrozen(obj)

如果无法在对象中修改现有属性的特性和值,且无法向对象添加新属性,则返回 true。

Object.isExtensible(obj)

如果可以向对象添加新属性,则返回true

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注