JavaScript's object-oriented experience [summarizing]_javascript skills

Source: Internet
Author: User
The object-oriented design of JavaScript is a bit far-fetched, after all, the JavaScript language itself has no high-level language rigor. Before reading this article, I hope you can understand C + +, Java, C # and other high-level languages, there is the basic knowledge of object-oriented programming.
1. Definition of class:
function ClassName () {
}
You can see that the class is defined in the same way as the function.
In fact, functions and classes can only be distinguished when used, for example, we use them in a single page:
<script language= "JavaScript" >
function A () {
Alert (' Hello ');
}

A as a function to use
A ();

A is used as a class to derive an Obj object from Class A
var obj=new A ();
</script>

2. Members of the class:
function ClassName () {
Defining Public variables
this.property1=0;
Defining public methods
This.method1=function () {
To determine whether THIS.A is assigned a value
if (this.a!= undefined)
alert (THIS.A);
}

Another way of defining a form
This.method3=funca;

Defining Private Members
var pram1=1;
var method2=function () {
Alert (")";
}
}
Funca is the processing function of the method method3
function Funca () {
Alert (")";
}
The above code slowly to explain to everyone:
All that begin with this are members of the class and are public.
For example: Property1 is the attribute of class, Method1 is the method of class;
Members of a class do not need to be defined with VAR, and private variables are not used without the this prefix.
For example: PRAM1 is a private variable, METHOD2 is a private method;
The properties of a class can not be defined in the class, and the properties that are not initialized can be undefined and can still be invoked elsewhere;
For example: In the method1 to output a property, in the entire class there is no definition of a property, we create the object can be assigned to the value.
var obj=new ClassName ()
Obj.a= "Hello JavaScript";
Obj.method1 ();
The methods of the class can be defined by This.method=function () {}, such as the Method1 method;
It can also be defined by This.method=funcname, which specifies the method to be handled by a function, such as method3;

3. Inheritance of classes:
function ClassA () {
this.property1= ' Hello ';
This.method1=function () {
alert (this.property1);
}
}

function ClassB () {
}
Inherit ClassA
Classb.prototype=new ClassA ();

Add Pi property to ClassB
classb.prototype.pi=3.1415926;
Add Showpi method to ClassB
Classb.prototype.showpi=function () {
Alert (this. PI);
}
By using the prototype object, the instance of the class ClassA is assigned to the prototype object, thus CLASSB inherits all the members of the ClassA;
For example: Classb.prototype=new ClassA ();
You can also add members to a class outside of the class by prototype (this is a function not available in other high-level languages);
For example: ClassB.prototype.PI and ClassB.prototype.showPI

4. Overloads of class methods:
Overloading of class methods is used more in constructors of classes, for example: There are two methods with the same name in the class and different parameters or different parameter types. JavaScript does not formally support class method overloading, and we can complete the overload of the class method with its Arguments property:
function ClassA () {
Get the number of parameters
Note that this.arguments.length is wrong.
var num=classa.arguments.length;

This.method1=function () {
if (num==0) {
Alert (0);
}
if (num==1) {
Alert (1);
}
}
}
The arguments property of a class or function name returns an array containing all the arguments;
For example: ClassA.arguments.length can get the number of parameters, Classa.arguments[0] Get the value of the first parameter
As needed, by getting the number of parameters or parameter values, the same function or class has different functions, such as:
var obj= new ClassA ();
OBJ.METHOD1 ()//Output 0
var obj1= new ClassA (5);
OBJ1.METHOD1 ()//Output 1

5. Definition of object:
Object is an instance of a class that defines a class as a template for an object
function A () {
This.a=1;
this.b=2;
This.add=function () {
return this.a+this.b;
}
}
Define an Object
var obj=new A ();
Assignment Class Properties
obj.a=5;
obj.b=6;
Calling class methods
var sum=obj.add ();

Another way to define a method:
var obj={
A:1,
B:2,
Add:function () {
return THIS.A + this.b;
}
}

The object defined by this method, together with the structure of its class, is suitable for a class that is used only once
When you define properties and methods, you do not need to use the This keyword, ":" Directly assigned to the value can be
But using attributes in a method requires the use of the This keyword

This article is only my personal experience and understanding, I guess I write and some books are not the same, I believe that after reading will soon understand. This article only plays a role in the purpose of the specific project needs to be specifically treated. All the examples involved are personally debugged and correct if there are any errors.
Related Article

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.