JS native Object-oriented end-of-life (handsome)

Source: Internet
Author: User

Closed Package    Basic Object-Oriented fundamentals

1.1 Closed Package

In the programming language, the so-called closure refers to the grammatical field in a particular area, with a continuous reference ( read/write ) A paragraph of the ability of a non-persistent variable value on an execution domain that is outside its own scope within the region. These non-persistent variables of the outer execution domain magically preserve their values when the closure was originally defined ( or created ) .

Vernacular: We can use a function to visit the other way to internal variables of a function is to close the packet.

Internal Variables is a local variable and we know that local variables are not to be used by others casually.

function Fun () {
var num = 10;
}
console. log (num); This is the wrong way. Num is a local variable

Test Questions

1 function Outerfun ()

2 {

3 Var a=0;

4 function Innerfun ()

5 {

6 a++;

7 alert (a);

8}

9 return innerfun; Watch this .

10}

One-to-one var obj=outerfun ();

-obj (); obj ();

var obj2=outerfun ();

Obj2 (); Obj2 ();

The result is : 1 2 1 2

Benefits of closures :

Pros: Do not generate global variables to implement property privatization.

Disadvantage: The data in the closure will be resident memory, which will be deleted if not used, which may cause memory overflow.

1.2 Objects (Object)

What is the object?

Basic Data Type string number boolean null undefined

Array Object

Object Data type: objects are data types with properties and methods

var num = 10; variables
var arr = []; Array
Arr.index = 10; an index property of an array of arr

But there's a problem, It is inappropriate to use an array when we want certain properties or methods.

Arr.lenght.

we want our own ID properties and methods. It must be an object to ask.

1.2.1 declaring Objects

we have two ways of declaring objects .

var obj = new Object ();

but we are more advocating the second approach: literal declaration of an object

var obj = {};

1.2.2 Working with Objects

1.2.3 Object Oriented

Object-oriented: Classes and objects

A class is an abstraction of an object, and an object is a concrete instance of a class

all things are object JavaScript All Objects

classes and objects

Object girlfriend

Xiao Hu do you have a target? a girlfriend .

is that the girl sitting next to you? A particular girlfriend .

do you like eating apples? Generic class

is the apple in your hand sweet? object specified (specific instance)

Object-oriented features

1. Abstraction

Abstraction is about ignoring aspects of a topic that are not related to the current goal, so that you can more fully focus on the aspects that are relevant to the current goal.

2. Encapsulation

encapsulation is the process and data is surrounded, access to data only through the defined interface

3. Inheritance

4. polymorphic

Polymorphism refers to two or more objects belonging to different classes, in a manner that responds differently to the same message (method invocation)

1.2.4 New

the new we often use the new keyword to declare the object

Javascript

the role of the new operator is to create an object instance. This object can be user-defined, or it can be a system-brought object with constructors.

The new keyword allows this to point to the object

The so-called " constructor "is actually a normal function, but the this variable is used internally . Using the new operator on a constructor enables you to generate an instance, and The This variable is bound to the instance object.

1.2.5 prototype

of Common the same Part

Main solution: Because the function is very much used, the repetition efficiency is too low.

class . prototype. method = function () {} specific format

The invariant properties and methods can be defined directly on the prototype object .

How to use:

class name . prototype. Method

JS native Object-oriented end-of-life (handsome)

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.