javascript call () 과 apply ()
2952 단어 JavaScript
그의 두 가지 차 이 는 콜 의 첫 번 째 매개 변 수 는 반드시 대상 (또는 함수) 이 어야 하고 나머지 매개 변 수 는 임의로 할 수 있다 는 것 이다.한편, apply 는 보통 두 개의 매개 변수 만 있 고 첫 번 째 는 대상 이나 함수 이 어야 하 며 두 번 째 는 아래 표 시 된 집합 이다.
예 를 들 면:
function dow(s){
document.write("<h1>"+s+"</h1>");
}
function Point(x,y){
this.x=x;
this.y=y;
this.toString=function(){
return "("+[x,y]+")";
}
}
function Vectory(x,y){
this.x=x;
this.y=y;
this.toString=function(){
return "("+[x,y]+")";
}
}
function add(x,y){
return new this.constructor(this.x+x,this.y+y);
}
var p=new Point(2,4);//
var v=new Vectory(3,5);
//call() call call , call , call
dow(p);
dow(add.call(p,3,5));// p add
dow(add.apply(p,[2,6]));// ,
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.