This article mainly introduces the JS object-oriented public, private, static properties and methods, and attached a detailed example, very detailed and comprehensive, here recommended to you, the need for small partners can refer to the
Now, JavaScript is a big line, JavaScript is a necessary language for Web site developers, but with the popularity and use of frameworks such as jquery, many people lack a deep understanding of native JavaScript and are accustomed to functional editing style, for closures , the prototype is always not clear the way unknown. For JS object-oriented, and to understand the JS object-oriented, you must first understand JS in what is public method, privileged method, static method
Method/Step
1. Public property and public method function User (name,age) {this. name = name; User.prototype.getName = function () {//Public method return this. name; \ var user = new User (' Fire Sea ', 26); Console.log (User.getname ()); Output:fire Zihai 2. Private properties and Methods
function User (name,age) {var name = name;//private Property var age = age; function Alertage () {//Private method alert (age); } alertage (age); Popup = var user = new User (' Fire Sea ', 26); 3. Static Properties and Methods
In PHP, the method that can be called without instantiating is called static method, JS is same, need not instantiate, that is, the object is manifested with the new operator, the method and property of the object can be called.
function User () {} user.age = 26; static Property User.myname = ' Fire Sea '; User.getname = function () {//static method returns this. MyName///If this.name is used here, the return will be user, all converted to MyName, Console.log (user. GetName ()); Output:fire Zihai 4. Privileged Methods
function User (name,age) {var name = name;//private Property var age = age; this. GetName = function () {//privileged method return name;//private properties and methods cannot use this call} var user = new User (' Fire Sea ', 26); Console.log (User.getname ()); Output:fire Zihai 5. Static Class
Use literal methods to create a
var user = {Init:function (name,age) {this. name = name; this. Age = age; }, Getname:function () {return this. name; } user.init (' Fire Sea ', 26); Console.log (User.getname ()); Output:fire Zihai 6. Calling rules for public methods
To invoke the public method, we must first instantiate the object
Public methods do not invoke the common and privileged methods in the public method, and you cannot use this to invoke static methods and properties, which must be called by the object itself, that is, the object name. Public methods also cannot invoke Private method function User () {this. myname = ' fire sea ';//public Property this. Age = 26; this. do = function () {//privileged method return this. myname+ ' Learn JS '; } user.eat = function (food) {return ' Dinner only ' +food ';} User.prototype.alertAge = function () {alert (this. age); User.prototype.alertDo = function () {alert (this. do ());//Invoke Privileged method} User.prototype.alertEat = function (food) {AL ERT (User.eat (food)); The call to static method//alert (This.ear (food)) can only be invoked through the object itself: This.eat is not a function} var user = new User (); User.alertage (); Alert:26 User.alertdo (); Alert:fire Zihai Learning js user.alerteat (' instant noodles ')//alert: Dinner only instant noodles 7. Calling rules for static methods
When you use a static method, you can invoke it without instantiating the object, and the object instance cannot invoke the static method of the object, only the static properties and methods of the instance itself
function User () {} user.age = 26; static Property User.myname = ' Fire Sea '; User.getname = function () {//static method return this. myname} var user = new User (); Console.log (User.getname); TypeError:user.getName is not a function User.supper = ' instant noodles '; User.eat = function () {return ' Dinner only ' + this. Supper;} user.eat (); Dinner only instant noodles static methods cannot invoke public, public, private, private, privileged, and prototype properties
function User () {this. myname = ' fire Sub Sea ';//public Property this. Age = 26; this. do = function () {//privileged method return this. myname+ ' Learn JS '; } User.prototype.alertAge = function () {//public method, also known as prototype Method alert (this. age); User.prototype.sex = ' Male '; Prototype Properties User.getname= function () {//static method return this. myname;} User.getage = function () {this. alertage (); } user.getdo = function () {return this. do ();}//console.log (User.getname ())//undefined//console.log (User.getdo ()) ;//typeerror:this.do is not a function//console.log (User.getage ())//typeerror:this.alertage are not a function 8. Privileged Party Invoking rules of the law
The privileged method calls the public method, the public property, through the object itself, calls the static methods and properties, and calls the private and private methods directly in the method body.
function User (girlfriend) {var girlfriend = girlfriend; function Getgirlfriend () {return ' My girlfriend ' +girlfriend+ ' is a beauty. ' ; this. myname = ' Fire Sea '; Public property this. Age = 26; this. do = function () {//privileged method return this. myname+ ' Learn JS '; this. alertage = function () {this. changeage ();//Privileged method invokes public method alert (this. age); this. alertgirlfriend = function () {alert (Getgirlfriend ());//Calling Private method} User.prototype.changeAge = function () {this. age =: var user = new User (' So-and-so '); User.alertage (); Alert:29 User.alertgirlfriend (); Alert: My girlfriend So-and-so is a beautiful woman. 9. Private Method
The private methods and properties of the object, which cannot be accessed externally, are not within the method that can this call the object's public method, the public property, the privileged method's
Function User (girlfriend) { var girlfriend = girlfriend this. myname = ' Fire sub-Sea ';//Public Properties this. Age = 26; function Getgirlfriend () { //this.myname;//This point to the Window object, not the user object, //This.myname = ' Fire Sea ', at this point is the Getgirfriend object. //If a method that does not exist in Getgirfriend is invoked through this, this will point to the Window object, only this calls the methods and properties that exist in the getgirlfriend. This will specify Getgirlfriend; Alert (user.eat (' bubble surface ')); Alert: Dinner only instant noodles } this. do = function () {//Privileged method return this. myname+ ' Learn JS '; } this. alertage = function () { this. changeage ();//Privileged method invokes public method alert (this. age); ; } this. alertgirlfriend = function () { getgirlfriend ();//Calling Private method }} user.eat = function (supper) { Return ' dinner only ' +supper;