Javascript object-oriented public, private, and static attributes and Methods _ javascript skills

Source: Internet
Author: User
This article mainly introduces the public, private, and static attributes and methods of js object-oriented objects in detail, and attaches a detailed example, which is very detailed and comprehensive. We recommend this article to you, if you have any need, you can refer to the following: javascript is a popular language. For website developers, javascript is a language that must be mastered. However, with the popularity and use of jquery and other frameworks, many people lack a deep understanding of native javascript, get used to the functional editing style, and are confused about closures and prototypes. js object-oriented is a poor use. To understand js object-oriented, you must first understand what is a public method, a privileged method, or a static method in js.

Method/step

1. Public attributes and public methods

Function User (name, age) {this. name = name; // public attribute this. age = age;} User. prototype. getName = function () {// public method return this. name;} var user = new User ('fire zihai ', 26); console. log (user. getName (); // output: fire zihai

2. Private attributes and Methods

Function User (name, age) {var name = name; // Private Attribute var age = age; function alertAge () {// Private method alert (age );} alertAge (age); // pop up 26} var user = new User ('fire zihai ', 26 );

3. Static attributes and Methods

In php, the method that can be called without instantiation is called a static method. In js, the object method and attribute can be called without instantiation. That is, the object method and attribute can be called by using the new operator to implement the object.

Function User () {} User. age = 26; // static attribute User. myname = 'fire zihai '; User. getName = function () {// static method return this. myname; // if this. name, which will return the User, and all use myname,} console. log (User. getName (); // output: fire zihai

4. Privileged Methods

Function User (name, age) {var name = name; // Private Attribute var age = age; this. getName = function () {// privileged method return name; // Private attributes and methods cannot use this call} var user = new User ('fire zihai ', 26 ); console. log (user. getName (); // output: fire zihai

5. Static class

For static methods and static attributes, we do not need to create them as in step 3. If you have read my article "How to Create Image Carousel in js ", you will be able to create it in a literal way.

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. Call rules for public methods

To call a public method, we must first instantiate the object.

In a public method, you cannot use this to call static methods and properties by calling the public attributes and privileged methods without this. You must call the static methods and properties by calling the object itself, that is, the object name. Public methods cannot call private methods either.

Function User () {this. myname = 'fire zihai '; // public attribute 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 (); // call the privileged method} User. prototype. alertEat = function (food) {alert (User. eat (food); // only static methods can be called through the object itself // alert (this. an error occurs when calling ear (food) like this: this. eat is not a function} var user = new User (); user. alertAge (); // alert: 26user. alertDo (); // alert: fire zihai learning jsuser. alertEat ('Instant noodle ') // alert: instant noodle only for dinner

7. Call rules for static methods

When using static methods, you can call them without instantiating objects. object instances cannot call static methods of objects, but can only call static attributes and methods of instances.

Function User () {} User. age = 26; // static attribute User. myname = 'fire zihai '; User. getName = function () {// static method return this. myname;} var user = new User (); console. log (user. getName); // TypeError: user. getName is not a functionuser. supper = 'noodle '; user. eat = function () {return 'dinner only '+ this. supper;} user. eat (); // only instant noodles for dinner

Static methods cannot call Public attributes, public methods, private methods, private attributes, privileged methods, and prototype attributes.

Function User () {this. myname = 'fire zihai '; // public attribute this. age = 26; this. do = function () {// privileged method return this. myname + 'learn js';} User. prototype. alertAge = function () {// public method, also called the prototype method alert (this. age);} User. prototype. sex = 'male'; // prototype property 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 is not a function

8. Call rules for privileged Methods

Privileged methods use this to call public methods and public properties, call static methods and properties through the object itself, and directly call private and private methods in the method body.

Function User (girlfriend) {var girlfriend = girlfriend; function getGirlFriend () {return 'My girlfriend '+ girlfriend +' is a pretty girl! ';} This. myname = 'fire zihai '; // public attribute this. age = 26; this. do = function () {// privileged method return this. myname + 'learn js';} this. alertAge = function () {this. changeAge (); // call the public method alert (this. age);} this. alertGirlFriend = function () {alert (getGirlFriend (); // call the private method} User. prototype. changeAge = function () {this. age = 29;} var user = new User ('xx '); user. alertAge (); // alert: 29user. alertGirlFriend (); // alert: My girlfriend is a pretty girl!

9. Private Method

The private methods and properties of an object cannot be accessed externally. In a method, this cannot call the public methods, public properties, or privileged methods of an object.

Function User (girlfriend) {var girlfriend = girlfriend; this. myname = 'fire zihai '; // public attribute this. age = 26; function getGirlFriend () {// this. myname; // The window object to which this points at this time, not the User object, // this. myname = 'fire zihai'. this points to the getGirFriend object. // If this is used to call a method that does not exist in getGirFriend, this will point to the window object. Only when this calls the method and attribute that exists in getGirlFriend, this will specify getGirlFriend; alert (User. eat ('Instant noodle '); // alert: only instant noodles for dinner} this. do = function () {// privileged method return this. myname + 'learn js';} this. alertAge = function () {this. changeAge (); // call the public method alert (this. age);} this. alertGirlFriend = function () {getGirlFriend (); // call the private method} User. eat = function (supper) {return 'dinner is only '+ supper;} var user = new User ('xx'); user. alertGirlFriend ();

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.