百科狗-知识改变命运!
--

TypedArray.from() - JavaScript TypedArray 对象

梵高1年前 (2023-11-21)阅读数 14#技术干货
文章标签数组

TypedArray.from()

TypedArray.from()方法从一个类数组或者可迭代对象中创建一个新类型数组。这个方法和Array.from()类似。

语法

TypedArray.from(source[, mapFn[, thisArg]])

where TypedArray is one of:

Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array

参数

source想要转换为类型数组的类数组或者可迭代对象。mapFn可选参数。如果指定了该参数,则最后生成的类型数组会经过该函数的加工处理后再返回。thisArg可选参数。执行mapFn函数时this的值。

返回值

一个新的TypedArray实例。

描述

TypedArray.from() - JavaScript TypedArray 对象

TypedArray.from()允许你从下面两者来创建数组:

  • 类数组对象(拥有一个length属性和若干索引属性的任意对象)
  • Set等)。

TypedArray.from()方法有一个可选参数mapFn,让你可以在最后生成的类型数组上再执行一次map方法后再返回。也就是说TypedArray.from(obj, mapFn, thisArg)TypedArray.from(Array.prototype.map.call(obj, mapFn, thisArg))是等价的。

from()length属性为1.

Array.from()TypedArray.from()之间有一些微妙的区别:

  • 如果|this|的值传递给TypedArray.from不是一个构造器,TypedArray.from将抛出{jsxref("TypeError")}},而Array.from默认将创建一个Array.
  • TypedArray.from使用[Put]rray.from使用[DefineProperty]].因此,当和handler.defineProperty.
  • from获得一个迭代器时,TypedArray一开始收集迭代器中的所有值,此时创建一个|this|的实例用于计数,然后在实例中设置值。Array.from设置每个从迭代器其中获取的值,最后设置它的长度。
  • Array.from获得一个不可迭代的类数组时, it respects holes,而TypedArray.from将确保结果是 dense.

示例

// Set (iterable object)
var s = new Set([1, 2, 3]);
Uint8Array.from(s);
// Uint8Array [ 1, 2, 3 ]

// String
Int16Array.from('123');                      
// Int16Array [ 1, 2, 3 ]

// Using an arrow function as the map function to
// manipulate the elements
Float32Array.from([1, 2, 3], x => x + x);      
// Float32Array [ 2, 4, 6 ]

// Generate a sequence of numbers
Uint8Array.from({length: 5}, (v, k) => k);    
// Uint8Array [ 0, 1, 2, 3, 4 ]

Polyfill

You can partially work around this by inserting the following code at the beginning of your scripts, allowing use of much of the functionality of from()in implementations that do not natively support it.

if (!Int8Array.__proto__.from) {
    (function () {
        Int8Array.__proto__.from = function (obj, func, thisObj) {

            var typedArrayClass = Int8Array.__proto__;
            if(typeof this !== 'function') {
                throw new TypeError('# is not a constructor');
            }
            if (this.__proto__ !== typedArrayClass) {
                throw new TypeError('this is not a typed array.');
            }
 
            func = func || function (elem) {
                    return elem;
                };

            if (typeof func !== 'function') {
                throw new TypeError('specified argument is not a function');
            }

            obj = Object(obj);
            if (!obj['length']) {
                return new this(0);
            }
            var copy_data = [];
            for(var i = 0; i 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)