当前位置:网站首页>JS prototype. slice. call(arguments); Convert pseudo array to array
JS prototype. slice. call(arguments); Convert pseudo array to array
2022-06-25 13:22:00 【wendyTan10】
( One ).Array.prototype.slice.call(arguments);
- slice(start, end) Method to extract a part of an array , And return the new array
// Read elements in the array :
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3); // [) Left closed right open interval
citrus Results output :
["Orange", "Lemon"]
- call() Function is used to call the current function function, And can use the specified object at the same time obj As this execution function intra-function this Pointer reference .
var obj = {
name: "wendy", age: 20};
function fun(a, b){
console.log(this.name);
console.log(a);
console.log(b);
}
// change this Referenced as obj, Pass two parameters at the same time
fun.call(obj, 12, true); // Li Si 12 true
var args = [].slice.call(arguments, 0);
arguments Is an object, not an array , At best, it is a pseudo array , And there is no such thing on the prototype chain slice This method .
3. [] Itself is also an object . And the array prototype chain has this slice This method .
/* The return value here is true*/
[].slice === Array.prototype.slice;
adopt call Explicit binding to implement arguments Disguised slice This method
quote hanyuntao Of Array.prototype.slice.call() The share of
( Two ). The method of converting the actual parameters of a function into an array
Method 1 :var args = Array.prototype.slice.call(arguments);
Method 2 :var args = [].slice.call(arguments, 0);
Method 3 :
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
Last , A general function converted into an array is attached
var toArray = function(s){
//try Statement allows us to define code blocks for error testing at execution time .
//catch Statements allow us to define when try When an error occurs in a code block , Code block executed .
try{
return Array.prototype.slice.call(s);
} catch(e){
var arr = [];
for(var i = 0,len = s.length; i < len; i++){
//arr.push(s[i]);
arr[i] = s[i]; // It is said that this is more than push fast
}
return arr;
}
}
边栏推荐
- 剑指 Offer 第 1 天栈与队列(简单)
- 字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
- 關於數據在內存中的存儲下
- Summary of leetcode linked list problem solving skills
- Configuring pytorch in win10 environment
- Accidentally modify or delete the system variable path to restore
- Of binary tree_ Huffman tree_ Huffman encoding
- C # switch between Chinese and English input methods
- ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
- 关于结构体,枚举,联合的一些知识
猜你喜欢

Analyse de l'optimisation de la réécriture des requêtes lazyagg de l'entrepôt

Stockage des données en mémoire

中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队

关于结构体,枚举,联合的一些知识
![[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box](/img/81/f8280f16efa314d736c3a869c32ef0.jpg)
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box

Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板

坡道带来的困惑

Summer Ending
![[flask tutorial] flask development foundation and introduction](/img/c4/fb80fbe6b563e3b304d59623ef6465.jpg)
[flask tutorial] flask development foundation and introduction

J2EE from entry to earth 01 MySQL installation
随机推荐
Accidentally modify or delete the system variable path to restore
三行代码简单修改jar包的项目代码
Back test of quantitative trading - tqzfuturerenkowavestrategy
ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
KVM 脚本管理 —— 筑梦之路
On the simple realization of Sanzi chess game and the method of judging the victory of n-zi chess
Openstack learning notes -grace component insight
Nova组件源码分析之冷迁移与Resize
On the realization of guessing numbers game
指针,它那些不得不说的题目
字符串各操作函数与内存函数详解
Conway's law can not be flexibly applied as an architect?
OpenStack学习笔记-Glance组件深入了解
Component: is to switch between multiple components
Lexical trap
15 basic SEO skills to improve ranking
OpenStack学习笔记之-Nova组件深入了解
关于数据在内存中的存储下
C # switch between Chinese and English input methods
[flask tutorial] flask development foundation and introduction