gpt4 book ai didi

javascript函数声明(this.method和method的区别?)

转载 作者:行者123 更新时间:2023-12-02 14:12:41 25 4
gpt4 key购买 nike

我制作了函数,但 cc 方法不是函数ba() 和 b.cc() 有什么区别?

function A(){
this.aa=function(){
console.log('11111');
};
cc=function(){
console.log('2222');
};
};
var b= new A();
b.aa();
b.cc();

最佳答案

如果你有一个对象原型(prototype)函数,所有变量都是函数的一部分,因此它只存在于函数内部。而 this 引用创建的对象:

function a(){
a=0;
//part of the function
this.a=1;
//part of the object
}
alert(new a(););
//will alert Object{a:1}

关于javascript函数声明(this.method和method的区别?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com