当前位置:网站首页>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;
}
}
边栏推荐
- [data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
- 初始c语言的知识2.0
- 解析數倉lazyagg查詢重寫優化
- leetcode:918. 环形子数组的最大和【逆向思维 + 最大子数组和】
- À propos du stockage des données en mémoire
- leetcode:456. 132 mode [monotone stack]
- Germany holds global food security Solidarity Conference
- [pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
- 中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队
- Some knowledge about structure, enumeration and union
猜你喜欢

It's an artifact to launch a website in a few minutes

药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质

Sword finger offer II 028 Flatten multi-level bidirectional linked list

.NET in China - What's New in .NET

Rust,程序员创业的最佳选择?

Pointer, it has to say that the subject

KDD 2022 | GraphMAE:自监督掩码图自编码器

《MongoDB入门教程》第01篇 MongoDB简介

Optimization of lazyagg query rewriting in parsing data warehouse

关于一道教材题的讲解
随机推荐
Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
Golang keyboard input statement scanln scanf code example
OpenStack学习笔记-Glance组件深入了解
Where is it safe to open an account for buying funds? Please give me your advice
坡道带来的困惑
Serenvlt first met
Configuring pytorch in win10 environment
Using swiper to realize seamless rotation of multiple slides
QT mouse tracking
leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
[pit avoidance refers to "difficult"] halfcheckedkeys rendering problem in semi selected status of antd tree
Storage related contents of data in memory
QT display ffmpeg decoded pictures
Solve the problem that yarn cannot load files in vs Code
WIN10环境下配置pytorch
leetcode:剑指 Offer II 091. 粉刷房子【二维dp】
汇编标志位相关知识点(连)
Explanation of a textbook question
OpenStack学习笔记(一)
leetcode:456. 132 模式【单调栈】