Javascript & OOP

Source: Internet
Author: User

Some time ago, I made a topic about JavaScript and OOP in the team. I finally remember to bring it here and share it!

1. Data Structure
The data in Javascript is concise. Simple data only includes undefined, null, Boolean, number, and string types, but there is only one type of complex data, that is, object. this is like the Chinese classical simple materialism, which classifies the most basic elements of the world as gold, wood, and fire. Other complex materials are composed of these five basic elements.

JavascriptThe code in is only reflected in one form, that is, function.

Any JavaScript identifier, constant, variable, and parameter is only one of the unfined, null, boolen, number, String, object, and function types, that is, the type indicated by the return value of typeof. There are no other types.

2. object.The so-called "objectization" means the ability to organize data and code into complex structures. In JavaScript, only the object type and function type provide the objectization capability.

The so-called "function's object-oriented ability" means that any function can dynamically add or remove attributes for it. These attributes can be simple, object, or other functions.
That is to say, a function has all the features of an object. You can use a function as an object. In fact, a function is an object, but a bracket "()" operator is added to an ordinary object. This operator is used to execute the function logic.
Javascript uses a form called JavaScript Object Notation (JSON), which translates into Chinese as "JavaScript Object Notation ".

VaR Company =
{
Name: "Microsoft ",
Product: "softwares ",
Chairman: {name: "Bill Gates", age: 53, married: true },
Employees: [{name: "angel", age: 26, married: false}, {name: "hanson", age: 32, marred: true}],

Readme: function () {document. Write (this. Name + "product" + this. Product );}

};
3. Oop
1) Encapsulation.
VaR Company = function (name, product ){
VaR temp = "Temp ";
This. Name = Name;
This. Product = product;
This. Chairman = {name: "Bill Gates", age: 53, married: true };
This. Employees = [{name: "angel", age: 26, married: false}, {name: "hanson", age: 32, marred: true}];

This. readme = function () {document. Write (this. Name + "product" + this. Product );};

};
VaR Company = New Company ("Microsoft", "inventory ");

2) inheritPrototype: Prototype originated from French. The software standards are translated as "prototype", which represents the initial form of things and also contains the meaning of models and templates. All function objects in Javascript have a prototype attribute. This prototype attribute is an object type object. Therefore, we can add any attributes and methods to this prototype object. Since prototype is the object's "prototype", all objects constructed by this function will have this "prototype" feature.

Function person (name) // base class Constructor
{
This. Name = Name;
};

Person. Prototype. sayhello = function () // Add method to prototype of the base class Constructor
{
Alert ("Hello, I'm" + this. Name );
};

Function employee (name, salary) // subclass Constructor
{
Person. Call (this, name); // call the base class Constructor
This. Salary = salary;
};

Employee. Prototype = new person (); // create a base class object as the prototype of the subclass.

Employee. Prototype. showmethemoney = function () // Add prototype to constructor to subclass
{
Alert (this. Name + "$" + this. Salary );
};

VaR billgates = new person ("Bill Gates"); // create a billgates object for the base class person
VaR stevejobs = new employee ("Steve Jobs", 1234); // create the stevejobs object of the subclass employee

Billgates. sayhello (); // call the prototype method directly through the object
Stevejobs. sayhello (); // directly call the prototype method of the base class through the subclass object!
Stevejobs. showmethemoney (); // directly call the prototype method of the subclass through the subclass object

3)
PolymorphismFunction shape (margin)

{
This. Margin = margin;
This. size = function () {document. Write ("I Am original. My margin is:" + this. Margin + '<br> ');};

This. Hello = function () {document. Write ("My margin is:" + this. Margin + '<br> ');}

};

Function circle (margin)
{
Shape. Call (this, margin );
This. size = function () {document. Write ("I am circle! My size is: "+ 3.14 * This. Margin * This. Margin + '<br> ');}

}
Circle. Prototype = new shape ();

Function rectangle (margin)
{
Shape. Call (this, margin );
This. size = function () {document. Write ("I am rectangle! My size is: "+ this. Margin * This. Margin + '<br> ');}

}
Rectangle. Prototype = new shape ();

VaR shape = new shape (100 );
Shape. Size ();

VaR Circle = new circle (100 );
Circle. Size ();
Circle. Hello ();

VaR rect = new rectangle (100 );
Rect. Size ();
Rect. Hello ();

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.