Sixth article JavaScript Object-oriented

Source: Internet
Author: User

One, closed package

Closures are blocks of code that can contain variables that are free (not bound to specific objects).

A "closure" is an expression (usually a function) that has multiple variables and the environment in which those variables are bound, and therefore these variables are also part of the expression.

A closure is a function, and it "remembers what's happening around". Represented as "another function" defined by "one function" body

Because the scope chain can only be found inward, the function internal variable cannot be obtained by default. A closure that gets the variables inside the function externally.

<script>  Name="aa";    Function F1 () {        var name="bb";        function F2 () {            alert (name);        }         return F2    }     var ret=F1 ();    RET (); </script>
Second, object-oriented

1, first introduce prototype

Each function is an object (function), which has a sub-object prototype object, and the class is defined in the form of a function. Prototype represents the prototype of the function, and also represents a collection of members of a class

When you create an instance object of a class from new, the members of the prototype object become members of the instantiated object.

1, the object is referenced by the class, only the function object can be referenced;

2, after the new instantiation, its members are instantiated, the instance object can be called.

At the same time, the function is an object, and the function object can be called without being instantiated if it declares the member directly.

Specific reference

http://baike.baidu.com/item/prototype/14335187

2. Prototypes

Object-oriented in JS

There is no class, only functions, functions can be constructed out of class

This is the equivalent of self in Python.

Create object requires new

The data here is encapsulated in the object.

Specify the following:

//represents the creation of a class Foo, and the construction method of the Foo class is createdfunction Foo (name) { This. Name=name;//Replace the following with a prototype.//this.func= function () {//alert (this. Name)//            }        }//The prototype is created so that the method is put into the class, and each object is called directly .Foo.prototype={func:function () {alert ( This. Name)}}//the second way of writing a prototypeFoo.prototype.func=function () {alert ( This. Name)}
<script>    function Foo (name,age) {        this. Name=name;        Thos. age = Age;    }     = {        func:function () {            returnthis. name+this. Age        }    obj1=new Foo ("Eric",  );    RET=obj1. Func ();    Console.log (ret)</script>
Object-oriented prototyping

Sixth article JavaScript Object-oriented

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.