前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用ES6新特性开发微信小程序(5)——内建对象的变化

使用ES6新特性开发微信小程序(5)——内建对象的变化

作者头像
极乐君
发布2018-02-05 17:00:10
1.2K0
发布2018-02-05 17:00:10
举报
文章被收录于专栏:极乐技术社区极乐技术社区

Object对象

  • Object.prototype.proto:对象具有属性proto,可称为隐式原型,一个对象的隐式原型指向构造该对象的构造函数的原型,这也保证了实例能够访问在构造函数原型中定义的属性和方法。
  • Object.assign(target, …sources):可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象。
  • Object.is(value1, value2)用来判断两个值是否是同一个值。
  • Object.setPrototypeOf(obj, prototype)将一个指定的对象的原型设置为另一个对象或者null(既对象的[[Prototype]]内部属性)。
代码语言:js
复制
let Shape = function() {}; 
let shape1 = new Shape(); 
console.log(shape1.__proto__ === Shape.prototype); // 输出: true
let shape2 = { width: 100, height: 200 }; 
let shape3 = Object.assign({}, shape2); 
console.log(shape3); // 输出: Object {width: 100, height: 200}
console.log(Object.is('Rectangle', 'Rectangle')); // 输出: true
console.log(Object.is('Rectangle', 'Circle')); // 输出: false
let shapeProto = {}; 
let shape4 = { width: 300, height: 400 }; 
Object.setPrototypeOf(shape4, shapeProto);
shapeProto.x = 10;
shapeProto.y = 20; 
console.log(`shape4 x is ${shape4.x}, y is ${shape4.y}`); // 输出: shape4 x is 10, y is 20

Array对象

  • Array.from(arrayLike[, mapFn[, thisArg]]):可以将一个类数组对象或可遍历对象转换成真正的数组。
  • Array.of(element0[, element1[, …[, elementN]]]):将它的任意类型的多个参数放在一个数组里并返回。
  • Array.prototype.copyWidthin(target[, start[, end]]):浅拷贝数组的部分元素到同一数组的不同位置,且不改变数组的大小,返回该数组。
  • Array.prototype.entries():返回一个 Array Iterator 对象,该对象包含数组中每一个索引的键值对。
  • Array.prototype.fill(value[, start = 0[, end = this.length]]):可以将一个数组中指定区间的所有元素的值, 都替换成或者说填充成为某个固定的值。
  • Array.prototype.find(callback[, thisArg]):如果数组中某个元素满足测试条件,find() 方法就会返回那个元素的第一个值,如果没有满足条件的元素,则返回 undefined。
  • Array.prototype.findIndex(callback[, thisArg]):用来查找数组中某指定元素的索引, 如果找不到指定的元素, 则返回 -1。
  • Array.prototype.keys():返回一个数组索引的迭代器。
  • Array.prototype.values():返回一个新的 Array Iterator 对象,该对象包含数组每个索引的值。
  • Array.prototype@@iterator:数组的 iterator 方法,默认情况下与 values() 返回值相同。
代码语言:javascript
复制
console.log(Array.from("foo")); // 输出: ["f", "o", "o"]
console.log(Array.from([1, 2, 3], x => x + x)); // 输出: [2, 4, 6]
console.log(Array.of(1)); // 输出: [1]
console.log(Array.of(1, 2, 3)); // 输出: [1, 2, 3]
console.log([1, 2, 3, 4, 5].copyWithin(-2)); // 输出: [1, 2, 3, 1, 2]
console.log([1, 2, 3, 4, 5].copyWithin(0, 2, 4)); // 输出: [3, 4, 3, 4, 5]
let arr1 = ["a", "b", "c"];    
let arrEntr1 = arr1.entries();    
console.log(arrEntr1.next().value); // 输出: [0, "a"]
console.log(arrEntr1.next().value); // 输出: [1, "b"]
console.log(arrEntr1.next().value); // 输出: [2, "c"]
console.log([1, 2, 3].fill(4)); // 输出: [4, 4, 4]
console.log([1, 2, 3].fill(4, 1, 2)); // 输出: [1, 4, 3]
console.log([4, 9, -25, 36].find((n) => n < 0)); // 输出: -25
console.log([4, 9, -25, 36].findIndex((n) => n < 0)); // 输出: 2
let arr2 = ["a", "b", "c"];    
let iter2 = arr2.keys();    
console.log(iter2.next()); // 输出: { value: 0, done: false }
console.log(iter2.next()); // 输出: { value: 1, done: false }
console.log(iter2.next()); // 输出: { value: 2, done: false }
console.log(iter2.next()); // 输出: { value: undefined, done: true }
let arr4 = ["a", "b", "c"];    
let iter4 = arr4[Symbol.iterator]();    
console.log(iter4.next().value); // 输出: a
console.log(iter4.next().value); // 输出: b
console.log(iter4.next().value); // 输出: c

String对象

  • String.fromCodePoint(num1[, …[, numN]]):返回使用指定的代码点序列创建的字符串。
  • String.raw(callSite, …substitutions):是一个模板字符串的标签函数,它的作用类似于 Python 中的字符串前缀 r 和 C# 中的字符串前缀 @,是用来获取一个模板字符串的原始字面量值的。
  • String.prototype.codePointAt(pos):返回 一个 Unicode 编码点值的非负整数。
  • String.prototype.endsWith(searchString [, position]):用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。
  • String.prototype.includes(searchString[, position]):用于判断一个字符串是否被包含在另一个字符串中,如果包含,就返回true;否则,返回false。
  • String.prototype.repeat(count):构造并返回一个重复当前字符串若干次数的新字符串。
  • String.prototype.startsWith(searchString [, position]):用来判断当前字符串是否是以另外一个给定的子字符串“开头”的,根据判断结果返回 true 或 false。
  • String.prototype@@iterator:返回一个新的Iterator对象,它遍历字符串的代码点,返回每一个代码点的字符串值。
代码语言:javascript
复制
console.log(String.fromCodePoint(42)); // 输出: *
console.log(String.fromCodePoint(65, 90)); // 输出: AZ
console.log(String.raw `Hi\n!`); // 输出: Hi\n!
let str1 = "Zhang san";    
console.log(String.raw `Hi\n${str1}!`); // 输出: Hi\nZhang san!
console.log('ABC'.codePointAt(0)); // 输出: 65
console.log('ABC'.codePointAt(1)); // 输出: 66
let str2 = `To be, or not to be, that is the question.`;    
console.log(str2.endsWith("question.")); // 输出: true
console.log(str2.endsWith("to be")); // 输出: false
console.log(str2.includes("question")); // 输出: true
console.log(str2.includes("To be", 2)); // 输出: false
console.log("abc".repeat(3)); // 输出: abcabcabc
console.log(str2.startsWith("To be")); // 输出: true
console.log(str2.startsWith("not to be")); // 输出: false
console.log(str2.startsWith("not to be", 10)); // 输出: true
let str3 = 'A\uD835\uDC68';    
let strIter3 = str3[Symbol.iterator]();    
console.log(strIter3.next().value); // 输出: A
console.log(strIter3.next().value); // 输出:

Number对象

  • Number.EPSILON:表示 1 和大于 1 的最小值(可表示为 Number)的差值。
  • Number.isFinite(value):用来检测传入的参数是否是一个有穷数(finite number)。
  • Number.isInteger(value):用来判断给定的参数是否为整数。
  • Number.isNaN(value):用来检测传入的值是否是 NaN。该方法比传统的全局函数 isNaN() 更可靠。
  • Number.isSafeInteger(testValue):用来判断传入的参数值是否是一个“安全整数”(safe integer)。
代码语言:javascript
复制
console.log(Number.EPSILON); // 输出: 2.220446049250313e-16
console.log(0.1 + 0.2 === 0.3); // 输出: false
console.log(Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON); // 输出: true
console.log(Number.isFinite(2e64)); // 输出: true
console.log(Number.isFinite(NaN)); // 输出: false
console.log(Number.isInteger(0)); // 输出: true
console.log(Number.isInteger(0.1)); // 输出: false
console.log(Number.isNaN(0 / 0)); // 输出: true
console.log(Number.isNaN(0.1)); // 输出: false
console.log(Number.isSafeInteger(3)); // 输出: true
console.log(Number.isSafeInteger(Math.pow(2, 53))); // 输出: false
console.log(Number.isSafeInteger(Math.pow(2, 53) - 1)); // 输出: true

Math对象

  • Math.acosh(x):返回一个数字的反双曲余弦值
  • Math.asinh(x):返回给定数字的反双曲正弦值
  • Math.atanh(x):返回一个数值反双曲正切值
  • Math.cbrt(x):返回任意数字的立方根
  • Math.cosh(x):返回数值的双曲余弦函数
  • Math.sign(x):用来判断一个数字的符号, 是正数, 负数, 还是零
  • Math.sinh(x):返回一个数字(单位为角度)的双曲正弦值
  • Math.tanh(x):返回一个数的双曲正切函数值
  • Math.trunc(value):将数字的小数部分去掉,只留整数部分
代码语言:javascript
复制
console.log(Math.acosh(1)); // 输出: 0
console.log(Math.acosh(2)); // 输出: 1.3169578969248166
console.log(Math.asinh(1)); // 输出: 0.8813735870195429
console.log(Math.asinh(0)); // 输出: 0
console.log(Math.atanh(0)); // 输出: 0
console.log(Math.atanh(0.5)); // 输出: 0.5493061443340548
console.log(Math.cbrt(27)); // 输出: 3
console.log(Math.cbrt(64)); // 输出: 4
console.log(Math.cosh(0)); // 输出: 1
console.log(Math.cosh(1)); // 输出: 1.5430806348152437
console.log(Math.sign(7)); // 输出: 1
console.log(Math.sign(0)); // 输出: 0
console.log(Math.sign(-7)); // 输出: -1
console.log(Math.sinh(0)); // 输出: 0
console.log(Math.sinh(1)); // 输出: 1.1752011936438014
console.log(Math.tanh(0)); // 输出: 0
console.log(Math.tanh(1)); // 输出: 0.7615941559557649
console.log(Math.trunc(13.37)); // 输出: 13
console.log(Math.trunc(0.123)); // 输出: 0

微信小程序目前不支持的API

  • Array.prototype.values()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-12-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 极乐技术社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Array对象
  • String对象
  • Number对象
  • Math对象
  • 微信小程序目前不支持的API
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档