2024. április 25., csütörtök

Gyorskeresés

javascript osztály, publikus/privát metódussal

Írta: | Kulcsszavak: javascript . oop . class . method . metódus . osztály . private . public

[ ÚJ BEJEGYZÉS ]

Ezeket mindig elfelejtem:

C = function() { //Begin of class C

var m1 = function(){
alert('m1');
};

m2 = function(){
alert('m2');
};


this.m3 = function(){
alert('m3');
};

this.m4 = function(){
m1();
m2();
//m3(); m3 is not a function
this.m3();
};




}; //End of class C

//m1(); //m1 is not defined
//m2(); //m2 is not defined
//m3(); //m3 is not defined

//C.m1(); //C.m1 is not a function
//C.m2(); //C.m2 is not a function
//C.m3(); //C.m3 is not a function

var c = new C();

//m1(); //m1 is not defined
//m2(); //OK "m2"
//m3(); //m3 is not defined

//c.m1(); //c.m1 is not a function
//c.m2(); //c.m2 is not a function
//c.m3(); //OK "m3"

c.m4(); //"m1","m2", "m3"

Tanulság:

Osztályt így írjunk:

ClassName = function(){


};

Publikus metódust az osztályon belül így:

this.m = function() {
};

Privát metódust az osztályon belül így:

var m = function(){

};

Publikus metódust az osztályon belülről is this-szel kell hívni.

Copyright © 2000-2024 PROHARDVER Informatikai Kft.