Small and miscellaneous JS object-oriented

Source: Internet
Author: User

Encapsulation: by declaring a method or attribute as private, you can keep the implementation details of the object confidential to other objects to reduce the coupling between objects, the data can be guaranteed and the modification method can be restricted, which makes the code more reliable and easier to debug. Encapsulation is the cornerstone of object-oriented design...

However, javascript does not have any internal mechanism to declare members as public or private, so we can only find a way to implement this feature.

Private attributes and Methods: The function has a scope. variables declared with var in the function cannot be accessed outside the function, private attributes and methods are essentially variables that you want to be inaccessible outside the object.

Privileged attributes and Methods: this keyword is used to create attributes and Methods. Because these methods are defined in the scope of constructors, they can access private attributes and methods, only methods that require direct access to private members should be designed as privileged methods.

Public attributes and Methods: attributes and methods defined on prototype are not allowed to access private members in the constructor, but to access privileged members. Subclass inherits all common methods and properties.

Common static attributes and methods: the best way to understand it is to think of it as a namespace, which is actually equivalent to using the constructor as a namespace.

01 // Encapsulation

02

03 var _ packaging = function (){

04 // Private attributes and Methods

05 var name = 'darren ';

06 var method = function (){

07

08 };

09 // privileged attributes and Methods

10 this. title = 'javascrput ';

11 this. getName = function () {return name ;};

12 };

13

14 // common attributes and Methods

15 _ packaging. prototype = {

16 init: function (){}

17 };

18

19 // common static attributes and Methods

20 _ packaging. _ name = 'darren ';

21 _ packaging. alertName = function () {alert (_ packaging. _ name );};

Inheritance: There are two methods to implement inheritance in javascript: class inheritance and original type inheritance. Example:

View sourceprint? 01/* -- class inheritance --*/

02 // declare a superclass first

03 function Person (name ){

04 this. name = name;

05}

06 // Add the getName Method to the prototype object of this superclass

07 Person. prototype. getName = function (){

08 return this. name;

09}

10 // instantiate this superclass

11 var a = new Person ('darren1 ')

12 alert (a. getName ());

13 // re-declare the class

14 function Programmer (name, sex ){

15 // This class calls the super class Person constructor and passes the parameter name to it

16 Person. call (this, name );

17 this. sex = sex;

18}

19 // The prototype object of this subclass is equal to the superclass instance.

20 Programmer. prototype = new Person ();

21 // prototype because the prototype object of the subclass is equal to the superclass instance. constructor: This method is also a super-class constructor. You can test it yourself. If this step is not completed, alert (Programmer. prototype. constructor). This is a reference to the Person superclass. Therefore, you must assign a new value to yourself.

22 Programmer. prototype. constructor = Programmer;

23 // The getSex method is added to the subclass.

24 Programmer. prototype. getSex = function (){

25 return this. sex;

26}

27 // instantiate This subclass

28 var _ m = new Programmer ('darren2', 'male ');

29 // Methods

30 alert (_ m. getSex ());

31 // inherit the superclass Method

32 alert (_ m. getName ());


View sourceprint? 01/* -- original type inheritance --*/

02 // use the clone () function to create a class Person object

03 var clone = function (obj ){

04 var _ f = function (){};

05 // This sentence is the core of the original type inheritance. The prototype object of the function is the object literal volume.

06 _ f. prototype = obj;

07 return new _ f;

08}

09 // declare an object literal

10 var Person = {

11 name: 'darren ',

12 getName: function (){

13 return this. name;

14}

15}

16 // you do not need to define a child class of Person. You only need to execute a clone operation.

17 var Programmer = clone (Person );

18 // you can directly obtain the default value provided by Person, or you can add or modify attributes and methods.

19 alert (Programmer. getName ())

20 Programmer. name = 'darren2'

21 alert (Programmer. getName ())

22

23 // declare the subclass and execute a clone.

24 var Someone = clone (Programmer );

Feng shangshi's blog

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.