I want to talk about the--javascript. Object-oriented (1)

Source: Internet
Author: User

There are objects in any language, except for the individual. Today, I'm talking about object-oriented in JavaScript.

What is object-oriented?

All the algorithms and logic in the computer are from the life, the object, this abstract concept is not the male understanding of the sister Ha, objects, everywhere. Like what......

Your computer keyboard is an object. We can use it to hit the text, then that's his way. And the method of Yong yuan is movement. Then it has a lot of buttons. Each of these keys corresponds to a different fixed content, such as button A, you can't press a letter B on his screen, except for poisoning. Then these buttons we call him attributes. The properties of perpetual transport are static.

Summarize ha: Object definition: There are two kinds: Object consumer, Object creator.

1, we do not know how his internal operation, but we will use, such as press A will appear a letter, we do not need to understand why this, but we will use the line. --The use of objects.

2, the composition of the object: Method (function)--movement; attribute (variable)--static;--the creator of the object;

3, Important features: inheritance. can also be called heredity.
The genetic a->b; inherits some methods, attributes, subclasses, and some other characteristics from the parent class.

Good, still do not understand, because I write too bad ha. Then I'll write it carefully.

The user of the object, in JS in fact there are a lot of JS to us to write good objects we just need to use the line. For example, array (), using the method: 1, var arr=new array (), 2, Var arr=[1,2,3,]. The basics are clear, huh, this is not an array, yes. But you have to think about why it is written well, you can have attributes length, and arr[0] subscript can be used, and the others do not? In fact, this is the JS write good object-oriented, we can use it as long as the initialization is used to encapsulate the method, but we never know what he did inside, but will use. This is the user.

Always use other people's things, not very good, so we have to write, the function of the package or object-oriented, and finally, you write out of that, you can also call plug-ins! Well, this is a lot of people understand. A set of methods, he has no specific use, but once I have the need, I can unlimited use it to deal with the same problem does not count the event.

Look at the code:

1. Function fn () {

Alert ("Songyusheng is the most handsome")

}

2. Var obj={

Fn:function () {

Alert ("and You Say")

}

}

1, function is a function, call method directly fn () just fine. 2, is the function in the object, call Method Obj.fn ();

Many people have doubts, what is the difference, there is a difference, if this obj is an acceptable dynamic parameter of this, then you can use this object arbitrarily.

Summary: 1. The function is free and can be called directly without a father. The method is also a function, but he belongs to an object, he has a father.

2. The attribute is also a variable, as with the method, which belongs to the object. Ok. That said, a lot of children should understand that ha, my writing is not too good. Slowly understand ha.

The object-oriented world has a factory model, which is not written here. Because of a deadly problem. He's going to cause performance degradation, why? Because every time he is used, he will produce a new function, so that if more, it will let the memory overflow. (If you want to know more, please q me.) Factory way, in fact you often write. is simply to pass the argument, apply. )

So, what about the children who insist on writing their own objects?

Some.

Write properties with constructor functions
To write a method in a prototype way
Initial capital letter of construction function

Oh, buy Karma! See the high-force lattice's name is not an instant urine. In fact, they are really simple.

Look at the code:

function Gouzao (NAME,QQ) {//write property with constructor
THIS.NAME=NAME,THIS.QQ=QQ;
}
gouzao.prototype.names= function () {//prototype write method
Alert (this.name)
};
gouzao.prototype.qqs= function () {//prototype write method
Alert (THIS.QQ)
};
var a=new gouzao ("Songyusheng", 289483936);
A.names ();
A.qqs ();
var b=new gouzao (' Solution pine ', 406842877);
B.names ();
B.qqs ();

If it is a Factory mode, then these two methods are two separate functions, if more, it will let performance down.

Now this, he's called mixed mode.

Write properties with constructor functions
To write a method in a prototype way
Initial capital letter of construction function

And prototype is for the expansion of the object, here no longer say, casually Baidu a bit can know ha.

Key knowledge: Understand what an object is. Concept and composition. Usage and notation. There are several modes.

We often write this also has a name:

Var json={

"Name": "Songyusheng",

"Age": "21 years old",

"Sex": "Male"

}

A JSON object. When we generally submit data to the server, we submit it in JSON format.

Ok, the above is my basic object of entry-level tyranny of the children of the bad text. (Big God, please cover your eyes)

Songyusheng is willing to tell you more.

I want to talk about the--javascript. Object-oriented (1)

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.