JavaScript design Pattern Learning--javascript The difference between object-oriented and Java

Source: Internet
Author: User

A comparison between static language and dynamic language object-oriented

  Packaging:

1) package data: Java through the private variables, such as the implementation of the local variables within the function of JavaScript implementation

2) Encapsulation implementation: Changes inside objects are transparent to the outside, and objects communicate through exposed interfaces

3) package Type: Java in the implementation of inheritance (abstract class) or interface inheritance (interface) to find ways to achieve the object type of hiding to achieve polymorphic effect, JavaScript does not have this problem;

4) Package Change: This is the purpose of the design pattern, encapsulating the parts of the code that are changing. There are three types of design patterns: The purpose of the creation pattern is to encapsulate the change of the created object, the composite relationship between the objects encapsulated by the structure pattern, and the behavioral pattern to encapsulate the behavior change of the object;

  Inherited:

    There are two ways of inheriting in Java: Implementing Inheritance (abstract Class), Interface Inheritance (interface), realizing polymorphism on one hand through abstract class or interface, and embodying a kind of contract binding protocol.

JavaScript inherits through the prototype chain implementation, does not support the abstract class or interface, JavaScript as a weak type language natural implementation of polymorphism, to reflect the contract agreement can not be like Java in the compilation of the active error, only at runtime or after the constraint;

 The different nature of inheritance is that they create objects in different ways

Languages such as Javac follow the design pattern of a template, creating an object by specifying its type, then creating the object through the class, assigning values to individual properties under the object, and so on.

Dynamic language creation objects such as JavaScript use prototype mode, the object system is built by prototype mode, in prototype mode, the class is not necessary, the object may not be created from the class, one object is obtained by cloning another object, The root object in JavaScript is Object.prototype, which is an empty object. Even though JavaScript provides a Java-like way to create objects, it is essentially the bottom-up that is produced by cloning. The key to the prototype pattern implementation is whether the language itself provides the Clone method, and ECMA5 provides a object.create method that can be used to clone an object, in a browser that does not support the method, or to produce an object using the following methods:

        object.creat=object.create| | function (obj) {                    var f=function () {};                    F.prototype=obj;                     return New F ();        }

The prototype pattern follows these rules:

1) All data are objects;

2) to get an object, not by instantiating an object, but by finding an object as a prototype and cloning it;

3) The object remembers its prototype (remember?) Each function in JavaScript has a prototype property that points to its object's prototype, each of which has a constructor object pointing to the corresponding constructor, and each object has a hidden property (under Chrome, the property is _proto_ ) points to the corresponding prototype object, that is, the object. _proto_ with the object constructor. Prototype the same point);

4) If the object cannot handle the current request, it will delegate the request to its prototype;

  Polymorphic:

(See the explanation of the package type in the package)

 

JavaScript design Pattern Learning--javascript The difference between object-oriented and Java

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.