欢迎光临,了解微信小程序开发,就上易用通!

使用ES6新特性开发微信小程序(5)

发布:2018-01-24 15:06浏览: 来源:网络 作者:tianshu

ES6对Object、Array、String、Number、Math等原生对象添加了许多新的API。

Object对象
  • Object.prototype.proto:对象具有属性proto,可称为隐式原型,一个对象的隐式原型指向构造该对象的构造函数的原型,这也保证了实例能够访问在构造函数原型中定义的属性和方法。
  • Object.assign(target, …sources):可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象。
  • Object.is(value1, value2)用来判断两个值是否是同一个值。
  • Object.setPrototypeOf(obj, prototype)将一个指定的对象的原型设置为另一个对象或者null(既对象的[[Prototype]]内部属性)。
  1. Object对象
  2.  
  3. Object.prototype.proto:对象具有属性proto,可称为隐式原型,一个对象的隐式原型指向构造该对象的构造函数的原型,这也保证了实例能够访问在构造函数原型中定义的属性和方法。
  4. Object.assign(target, …sources):可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象。
  5. Object.is(value1, value2)用来判断两个值是否是同一个值。
  6. Object.setPrototypeOf(obj, prototype)将一个指定的对象的原型设置为另一个对象或者null(既对象的[[Prototype]]内部属性)。
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 方法,默认情况下与 values() 返回值相同。
  1. console.log(Array.from("foo")); // 输出: ["f", "o", "o"]
  2.     console.log(Array.from([1, 2, 3], x => x + x)); // 输出: [2, 4, 6]
  3.     console.log(Array.of(1)); // 输出: [1]
  4.     console.log(Array.of(1, 2, 3)); // 输出: [1, 2, 3]
  5.     console.log([1, 2, 3, 4, 5].copyWithin(-2)); // 输出: [1, 2, 3, 1, 2]
  6.     console.log([1, 2, 3, 4, 5].copyWithin(0, 2, 4)); // 输出: [3, 4, 3, 4, 5]
  7.     let arr1 = ["a", "b", "c"];
  8.     let arrEntr1 = arr1.entries();
  9.     console.log(arrEntr1.next().value); // 输出: [0, "a"]
  10.     console.log(arrEntr1.next().value); // 输出: [1, "b"]
  11.     console.log(arrEntr1.next().value); // 输出: [2, "c"]
  12.     console.log([1, 2, 3].fill(4)); // 输出: [4, 4, 4]
  13.     console.log([1, 2, 3].fill(4, 1, 2)); // 输出: [1, 4, 3]
  14.     console.log([4, 9, -25, 36].find((n) => n < 0)); // 输出: -25
  15.     console.log([4, 9, -25, 36].findIndex((n) => n < 0)); // 输出: 2
  16.     let arr2 = ["a", "b", "c"];
  17.     let iter2 = arr2.keys();
  18.     console.log(iter2.next()); // 输出: { value: 0, done: false }
  19.     console.log(iter2.next()); // 输出: { value: 1, done: false }
  20.     console.log(iter2.next()); // 输出: { value: 2, done: false }
  21.     console.log(iter2.next()); // 输出: { value: undefined, done: true }
  22.     let arr4 = ["a", "b", "c"];
  23.     let iter4 = arr4[Symbol.iterator]();
  24.     console.log(iter4.next().value); // 输出: a
  25.     console.log(iter4.next().value); // 输出: b
  26.     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对象,它遍历字符串的代码点,返回每一个代码点的字符串值。
  1. console.log(String.fromCodePoint(42)); // 输出: *
  2.     console.log(String.fromCodePoint(65, 90)); // 输出: AZ
  3.     console.log(String.raw `Hi\n!`); // 输出: Hi\n!
  4.     let str1 = "Zhang san";
  5.     console.log(String.raw `Hi\n${str1}!`); // 输出: Hi\nZhang san!
  6.     console.log('ABC'.codePointAt(0)); // 输出: 65
  7.     console.log('ABC'.codePointAt(1)); // 输出: 66
  8.     let str2 = `To be, or not to be, that is the question.`;
  9.     console.log(str2.endsWith("question.")); // 输出: true
  10.     console.log(str2.endsWith("to be")); // 输出: false
  11.     console.log(str2.includes("question")); // 输出: true
  12.     console.log(str2.includes("To be", 2)); // 输出: false
  13.     console.log("abc".repeat(3)); // 输出: abcabcabc
  14.     console.log(str2.startsWith("To be")); // 输出: true
  15.     console.log(str2.startsWith("not to be")); // 输出: false
  16.     console.log(str2.startsWith("not to be", 10)); // 输出: true
  17.     let str3 = 'A\uD835\uDC68';
  18.     let strIter3 = str3[Symbol.iterator]();
  19.     console.log(strIter3.next().value); // 输出: A
  20.     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)。
  1.     console.log(Number.EPSILON); // 输出: 2.220446049250313e-16
  2.     console.log(0.1 + 0.2 === 0.3); // 输出: false
  3.     console.log(Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON); // 输出: true
  4.     console.log(Number.isFinite(2e64)); // 输出: true
  5.     console.log(Number.isFinite(NaN)); // 输出: false
  6.     console.log(Number.isInteger(0)); // 输出: true
  7.     console.log(Number.isInteger(0.1)); // 输出: false
  8.     console.log(Number.isNaN(0 / 0)); // 输出: true
  9.     console.log(Number.isNaN(0.1)); // 输出: false
  10.     console.log(Number.isSafeInteger(3)); // 输出: true
  11.     console.log(Number.isSafeInteger(Math.pow(2, 53))); // 输出: false
  12.     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):将数字的小数部分去掉,只留整数部分
  1. console.log(Math.acosh(1)); // 输出: 0
  2.     console.log(Math.acosh(2)); // 输出: 1.3169578969248166
  3.     console.log(Math.asinh(1)); // 输出: 0.8813735870195429
  4.     console.log(Math.asinh(0)); // 输出: 0
  5.     console.log(Math.atanh(0)); // 输出: 0
  6.     console.log(Math.atanh(0.5)); // 输出: 0.5493061443340548
  7.     console.log(Math.cbrt(27)); // 输出: 3
  8.     console.log(Math.cbrt(64)); // 输出: 4
  9.     console.log(Math.cosh(0)); // 输出: 1
  10.     console.log(Math.cosh(1)); // 输出: 1.5430806348152437
  11.     console.log(Math.sign(7)); // 输出: 1
  12.     console.log(Math.sign(0)); // 输出: 0
  13.     console.log(Math.sign(-7)); // 输出: -1
  14.     console.log(Math.sinh(0)); // 输出: 0
  15.     console.log(Math.sinh(1)); // 输出: 1.1752011936438014
  16.     console.log(Math.tanh(0)); // 输出: 0
  17.     console.log(Math.tanh(1)); // 输出: 0.7615941559557649
  18.     console.log(Math.trunc(13.37)); // 输出: 13
  19.     console.log(Math.trunc(0.123)); // 输出: 0
微信小程序目前不支持的API
  • Array.prototype.values()


完整代码:https://github.com/guyoung/GyWxappCases/tree/master/ES6



免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。