JS join()
**join()**
方法将数组中所有元素连接为一个字符串。这个方法的算法和Array.prototype.join()
相同。 TypedArray 是这里的 类型化数组 之一。
语法
typedarray.join([separator = ',']);
参数
separator
可选。指定分隔每个元素的字符串。分隔符按需转换为字符串。如果没有,类型化数组的元素会以逗号(",")分隔。
返回值
所有元素连接后的字符串。
示例
var uint8 = new Uint8Array([1,2,3]);
uint8.join(); // '1,2,3'
uint8.join(' / '); // '1 / 2 / 3'
uint8.join(''); // '123'
Polyfill
由于没有名为_TypedArray_的全局元素,polyfill 必须"按情况"实现。
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.join
if (!Uint8Array.prototype.join) {
Object.defineProperty(Uint8Array.prototype, 'join', {
value: Array.prototype.join
});
}
如果你需要支持过时的 JavaScript 引擎,它们不支持[Object.defineProperty]
,最好不要 polyfill Array.prototype
方法,因为你不能使它们不可枚举。
规范
Specification | Status | Comment |
---|---|---|
[ECMAScript 2015 (6th Edition, ECMA-262) | ||
TypedArray.prototype.join](https://www.ecma-international.org/ecma-262/6.0/#sec-%typedarray%.prototype.join) | Standard | 初始定义。 |
[ECMAScript Latest Draft (ECMA-262) | ||
TypedArray.prototype.join](https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.join) | Draft |
浏览器兼容性
We’re converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven’t yet converted the data it contains.
- Desktop
- Mobile
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 37 (37) | 未实现 | 未实现 | 未实现 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 未实现 | 未实现 | 37.0 (37) | 未实现 | 未实现 | 未实现 |