当前位置:网站首页>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;
}
}
边栏推荐
- [pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
- JVM parameter interpretation
- Cold migration and resize of Nova component source code analysis
- 字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
- leetcode:456. 132 mode [monotone stack]
- Uncover gaussdb (for redis): comprehensive comparison of CODIS
- Koa frame
- [pit avoidance means "difficult"] antd select fuzzy search
- [machine learning] model and cost function
- Used in time filter (EL table)
猜你喜欢
On the simple realization of Sanzi chess game and the method of judging the victory of n-zi chess
Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
Stockage des données en mémoire
Capabilities required by architects
Leetcode: Sword finger offer II 091 Painting house [2D DP]
初始c语言的知识2.0
Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon
[machine learning] parameter learning and gradient descent
Seven competencies required by architects
随机推荐
1251- client does not support authentication protocol MySQL error resolution
剑指 Offer 第 1 天栈与队列(简单)
初始c语言时的一些知识
Back test of quantitative trading - tqzfuturerenkowavestrategy
[pit avoidance means "difficult"] actionref current. Reload() does not take effect
Download File blob transcoding
Explanation of a textbook question
Accidentally modify or delete the system variable path to restore
药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
MySQL 学习笔记
Where is it safe to open an account for buying funds? Please give me your advice
OpenStack学习笔记之-Nova组件深入了解
Online service emergency research methodology
Introduction to string 18 lectures Collection 4
Serevlt初识
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon
QT display ffmpeg decoded pictures
And console Log say goodbye
About data storage in memory