Javascript get name of instance of class

前端 未结 3 2068
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 04:44
function myClass(a,b,c) {
     this.alertMyName=function(){alert(instancename)}

{...}


}

and then

foo = myClass(a,b,c);
boo = myC         


        
3条回答
  •  盖世英雄少女心
    2020-12-12 05:23

    You could bring it in as a parameter:

    function myClass(name, a, b, c) {
       this.alertMyName = function(){ alert(name) }
    }
    
    foo = new myClass('foo', a, b, c);
    

    Or assign it afterwards:

    function myClass(a, b, c) {
       this.setName = function(name) {
           this.name = name;
       }
       this.alertMyName = function(){ 
           alert(this.name)
       }
    }
    
    foo = new myClass( a,b,c);
    foo.setName('foo');
    

提交回复
热议问题