javascript函数声明(这两者的区别.方法和方法?)

javascript function Declaration (the diffrence beween this.method and method?)

本文关键字:方法 区别 声明 函数 javascript      更新时间:2023-09-26

我创建函数,但cc方法不是函数b.aa()和b.cc()有什么区别?

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

如果你有一个对象原型函数,所有变量都是函数的一部分,所以它只存在于函数内部。而this是指创建的对象:

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