当前位置:网站首页>Function function
Function function
2022-07-28 23:28:00 【Corner sheep】
List of articles
Function
Every JavaScript Functions are actually one Function object . function (function(){}).constructor === Function // true We can get this conclusion .
Instance attributes
arguments
Is an array of class objects corresponding to the parameters passed to the function .
function func1(a, b, c) {
console.log(arguments[0]);
// expected output: 1
console.log(arguments[1]);
// expected output: 2
console.log(arguments[2]);
// expected output: 3
}
func1(1, 2, 3);
Function.length
length Property indicates the number of formal parameters of the function .
function func1() {
}
function func2(a, b) {
}
console.log(func1.length);
// expected output: 0
console.log(func2.length);
// expected output: 2
Example method
Function.length
length Property indicates the number of formal parameters of the function .
function func1() {
}
function func2(a, b) {
}
console.log(func1.length);
// expected output: 0
console.log(func2.length);
// expected output: 2
apply() The grammar of
call And apply The context of the setting function used for display , Both methods work the same way by binding objects to this, It's just different in passing parameters .
call and apply It's all about change this The direction of . The effect is the same , It's just that the way to pass the parameters is different .
call You can receive a list of parameters
apply Accept only one parameter array .
And bind Different call/apply The function will be executed immediately
function show(title) {
console.log(`${
title + this.name}`);
}
let lisi = {
name: " Li Si ",
};
let wangwu = {
name: " Wang Wu ",
};
show.call(lisi, " user 1:");
show.apply(wangwu, [" user 2:"]);
// user 1: Li Si
// user 2: Wang Wu
Function.prototype.bind()
bind() Method creates a new function , stay bind() When called , Of this new function this Is specified as bind() The first parameter of , The rest of the parameters will be the parameters of the new function , For calling .
const module = {
x: 42,
getX: function () {
return this.x;
},
};
const unboundGetX = module.getX;
console.log(unboundGetX()); // The function gets invoked at the global scope
// expected output: undefined
const boundGetX = unboundGetX.bind(module);
console.log(boundGetX());
// expected output: 42
Function.prototype.call()
call() Method uses a specified this Value and one or more parameters given separately to call a function .
function Product(name, price) {
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = "food";
}
console.log(new Food("cheese", 5).name);
// expected output: "cheese"
Function.prototype.toString()
toString() Method returns a string representing the source code of the current function .
function sum(a, b) {
return a + b;
}
console.log(sum.toString());
// expected output: "function sum(a, b) {
// return a + b;
// }"
console.log(Math.abs.toString());
// expected output: "function abs() { [native code] }"
Function.prototype.bind()
Function.prototype.bind()
边栏推荐
- (important) first knowledge of C language -- function
- Solve the problem of using anonymous users in pod due to the failure of attaching ciphertext token files for serviceaccount user authentication
- 【C语言】三子棋小游戏实现
- Sdwebimage source code combs 5 author motivation, modifies directory, and changes inheritance relationship
- MySQL常用的日期时间函数
- 参加竞赛同学们的留言 : 第十七届的记忆
- Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?
- High quality subroutine 2 - high cohesion
- Messages from students participating in the competition: memories of the 17th session
- Anr questions often asked in Android interviews
猜你喜欢
如何在VR全景中嵌入AI数字人功能?打造云端体验感
RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块
Date time functions commonly used in MySQL
6 个超级良心的开源教程!
Advanced C language: pointer (3)
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
Media query adaptation
MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
Programmer growth Chapter 30: artifact of identifying true and false needs
The front mounted ADAS camera in parking increased by 54.15% year-on-year, with TOP10 suppliers taking the lead
随机推荐
Kotlin function nesting
The applet vant webapp component is missing, and the referenced component reports an error
WebView whitelist
Hbuilderx shortcut key
Recurrent neural network (RNN)
Input element label
Media query adaptation
PCA learning
Text is hidden beyond and ellipsis is displayed
[image segmentation] vein segmentation based on directional valley detection with matlab code
High quality programming
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
Seagate released a new risc-v architecture processor: the performance of mechanical hard disk soared 3 times
RouterOS 有限dns劫持及check
二叉搜索树
General principles of software quality
c语言进阶篇:指针(二)
[physical application] Wake induced dynamic simulation of underwater floating wind turbine wind field with matlab code
How strong is this glue?
How to automatically install homebrew in China (domestic address)