Object-oriented JavaScript

Source: Internet
Author: User

Polymorphic

The idea behind polymorphism is to separate "what to do" and "who to do and how to do", that is, to separate "unchanging things" from "mutable things".

The most fundamental function is to eliminate these conditional branching statements by translating the conditional branching statements into the polymorphism of the objects.

Packaging

The goal is to hide information.

Encapsulating data:

JS depends on the scope of the variable to implement the encapsulation feature, and can only simulate the public and private properties of the two

var MyObject = (function () {var _name = "Salody";  Private variable return {getname:function () {   //public method return _name;}}) (); Console.log (Myobject.getname ());   Output: Salodyconsole.log (myobject._name);         Output: undefined

  

Prototype mode

From the point of view of design patterns, a prototype pattern is a pattern used to create an object, and if we want to create an object, one way is to specify its type first, and then create the object through the class. Prototype mode Choose another way, we don't care about the specific type of object, we find an object, and then we create an identical object by cloning .

A object.create method is provided in ECMASCRIPT5 that can be used to clone an object

The real purpose of prototype mode is to provide a convenient way to create objects of a certain type, and cloning is just the process and means of creating this object.

1 varPlane =function () {2      This. Blood = 100;3      This. Attacklevel = 1;4      This. Defencelevel = 1;5 }6 7 varPlane =NewPlane ();8Plane.blood = 500;9Plane.attacklevel = 10;TenPlane.defencelevel = 3; One  A varCloneplane =object.create (plane); - Console.log (cloneplane); -  the //for browsers that do not support the Object.create method, use the following code -Object.create = Object.create | |function(obj) { -     varF =function () {}; -F.prototype =obj; +  -     return NewF (); +}

Object-oriented JavaScript

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.