Js Object-Oriented Programming-Advanced content

Source: Internet
Author: User
For example, if A writes A class to B and B finds that the function of the class written by A is insufficient, but B does not have the right to rewrite the file of A, what should I do?

JavaScript object-oriented

16. dynamically add attributes or methods for each object

Function: returns a reference to an object type prototype.

  • Use of prototype

Format: class. prototype

Scenario:

For example, if A writes A class to B and B finds that the function of the class written by A is insufficient, but B does not have the right to rewrite the file of A, what should I do?

Function Person (name1, age1 ){

This. name = name1;

This. age = age1;

}

Var p1 = new Person ('hangsan', 30 );

P1.sex = 'male'

B thinks that this class should also have a sex attribute:


Function Preson (name, age) {this. name = name; This. age = age;} var p1 = new Person ('hangsan', 30); var p2 = new Person ('lisi', 20 ); // p2 does not have the sex property // The person class object that will be instantiated in the future will automatically have the sex property // even the person class object created earlier will automatically have the sex property // For the Person class added a member property Person called sex. prototype. sex = 'male'; Person. prototype. say = function () {// adds a say member method alert (this. name + this. age + this. sex );};
In actual development, the class defines Question 1 .:


What is the difference between this. speak = function () {} And person. prototype. speak = function?

The above two methods finally add a method called speak to the Person class.

First: Define the member method in the class, so programs using this class will automatically have this method

Second: defines the member method outside the class. Only the current application can use the speak method.

Question 2 .:

L extend the functions of the array class:

Add a method for each array object to find the location of an element

Array --> System Custom class

Question 3:

Extended numeric functions:

Add a method to a numeric object. The parameters of this method are integers of any number, add all parameters together, and return

VII. Use of Object classes

In Js, objects are the base classes of all classes. When you use Object classes to create custom objects, you do not need to define constructors.

For example, in the past, we had to define a person, and we had to define the person class.

Function Person (){

}

However, we can see that this class is empty and there is no attribute or method in it. Generally

The object of this class generally belongs to and is added after the method.

Since this class is empty, we consider using the Object class.

When defining a class Object, you only need to store multiple data without considering the class type. You can consider using the Object class

For example, the Object class has a method:

HasOwnProperty (property); determines whether an object has an attribute.

This method is applicable to all objects in the object class.

# P # js tutorial-Object-Oriented Programming # e #

18. Static attributes of simulation classes in javascript

In some object-oriented languages, you can use the static keyword to define static attributes or methods of a class. This can be simulated in JavaScript.

Syntax: Class Name. attribute name

For example:

Math. random (); // All static members are called using the class name.

19. Simulation class private attributes

  • In the object-oriented thinking, for some sensitive members who do not want to disclose them, they can be defined as private members. In JavaScript, this function can be simulated.

Syntax:

Function Person (p_name ){

Var name = p_name; // Add var before defining attributes in the class to indicate private

// Private Members cannot be accessed outside

// If You Want To set Private member attribute values, you can use

// Parameters passed by the constructor

}

20. Inheritance of simulation classes

L Syntax:

Object. prototype. ext = function (parObject ){

For (var I in parObject ){

This [I] = parObject [I];

}

};

Under what circumstances should inheritance be used?

There is a class that we can use, but the function of this class is not perfect, we can not modify its source code, so we need to inherit this class in our class

Example:

Function Person ()

{

This. name

This. age

This. say = function

}

However, we want to complete the description of a student. At this time, there is no student ID in the Person class, so we need to inherit the Person;

21. javascript Closure

A closure refers to an expression (usually a function) with many variables and an environment bound to these variables. Therefore, these variables are part of the expression.

When to use closure?

When you want to access the internal variables of a function

Keep the internal variables of the function in the memory.

Function test1 (){

I = 100;

Function test2 (){

Alert (I ++ );

}

Return test2; return the first address of the test2 function to a global variable.

The variable I is used here, so the value of I will not disappear. This is the closure.

}

Var result = test1 ();

Result ();

22. try... Catch statement

  • We can add try... catch statements so that more appropriate measures can be taken when an error occurs.

Syntax:

Try {

// The statement to be executed

// When these statements have errors, they will be caught by the catch command to execute the specified content in the catch statement.

}

Catch (error ){

}

Sometimes, we may intentionally let the program catch statement run

Throw statement

  • The role declared by throw is to create an exception ). You can use this declaration with the try... catch declaration to control program streams and generate precise error messages.

23. About debugging

1) set the breakpoint: The program runs here and stops automatically.

2) use Statement by statement: When a function is encountered, it will enter the function's internal

3) process-by-process: When a function is encountered, the running result of the function is taken directly, instead of entering the Function

24. JavaScript scope chain

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.