JavaScript Advanced Chapter Closure, simulation class, Inheritance (v) _javascript tips

Source: Internet
Author: User
Tags closure
A, JavaScript in the closure
1, we first understand what is the scope of the function.

2, the object of the call

Combined Example:

Copy Code code as follows:

function display (something)
{
function ExecuteDisplay1 ()
{
document.write ("I'm helping the boss print:" +something+ "<br/>");//reference the something parameter of the external function
}
The ExecuteDisplay1 ()//function display refers to an intrinsic function
}
Display ("Sorry"), after completion, is reclaimed by the garbage collector

3, closure of the formation of

Example One,

Copy Code code as follows:

var obj = {};//Global object
function Buyhouse (Price,area)
{
return function () {return "The room you want to pay:" +price*area;}; Take the intrinsic function as the return value
}
Obj.people = Buyhouse (12000,80); Saves the reference of the intrinsic function to the people property of the Obj object.
This creates a closure, a simple expression: to save a reference to a nested function in a global scope, either by using the value it returns, or by having it exist in the object's properties.
document.write (Obj.people () + "<br/>");

Cases II,
Copy Code code as follows:

function Add ()
{
var number = 0;
return function () {return ++number;};/ /
}
var num = Add ();/Is there a 4 reference now? The first global creation: The Access function, the second has an external function (here refers to the add () referencing the anonymous function)
The third is the anonymous function (the return function ...). Refers to the local variable of add, and the fourth is the global object (Var num).
The objects for each invocation of the global object are still kept in the body of the function, so the value of the local variable will be maintained.
document.write (num ());

The equivalent method
Num2 = (function () {var number = 0;return function () {return ++number;}}) ();//anonymous function, directly assigned to global object
document.write (num2 ());

Example of implementing private properties
Copy Code code as follows:

Using closures to implement private properties
function CreateProperty (O,propertyname,check)
{
var value;
o[' Get ' +propertyname] = function () {return value;};/ /Returns an anonymous function body to the object's properties
o["Set" +propertyname] = function (v) {if (check &&!check)//Check the legality of the parameter throw ("Wrong parameter!");
else value = V; };//returns an anonymous function body to the object's properties
}
var o = {};
CreateProperty (O, "age", function (x) {return typeof x = = "number";}); /followed by an anonymous function that performs the work of validation and returns False if it is not a number
O.setage (22);//using the properties of the object
document.write (O.getage ());

In fact, the above still saves the function to the properties of the global object.

Second, the class in JavaScript
Also start with some basic terminology!
1. prototype (prototype)

In fact, the prototype of an object is the prototype value of the constructor, all functions are a prototype attribute, and when the function is created, it is automatically created and initialized, and the value initialized is an object, and the object comes with a property that is constructor. It refers back to the constructor associated with the prototype.
Copy Code code as follows:

function Peoplehope (money,house)
{
This.money = money;
This.house = House;
}
PeopleHope.prototype.hope = function () {document.write ("I want to own money, house");};/ /This is the prototype, which is initialized to the property of the object by the constructor.
For (var p in Peoplehope.prototype)
{
document.write ("The prototype came out!") \ t "+ p +" <br/> ");/output: Prototype out! Hope
}

2. Simulation class

In fact, the "class" in JavaScript is just a function. Go directly to the code!
Copy Code code as follows:

function Peoplehope (money,house)
{
This.money = money;
This.house = House;
Peoplehope.version = Properties of 0.1//class
peoplehope.createlive = function () {document.write ("under the leadership of the party, our life is very good!") ");} Class method must be a class direct reference
}

3. Inheritance of Class
Copy Code code as follows:

function Createclass (name,version)
{
THIS.name = name; Initializing Object Properties
this.version= version;
Createclass.author = "Frank";//Class properties
Createclass.sellhouse = function () {document.write ("We are real estate leading enterprise Vanke");};/ /class method
CreateClass.prototype.Company = "Vanke";
CreateClass.prototype.HousePrice = function () {document.write ("Big Mui Sha Peak Mansion sells 50,000,001 sets, the best-selling price!") ");};
Prototype, in fact, everyone here may ask, what is the difference between this prototype and the class method?
In fact, such as: var o = new Createclass ("Cofco Real Estate", "one issue"); This is o in the Createclass function.
O.name = "Cofco real estate"; o.version = "one issue";
As for what the prototype is actually doing, you can interpret it as a "traitor" when you create the object of O, the prototype tells the constructor to take away the initialization.
Into the properties of Object O.
}
function House (name,version,city)
{
Createclass.apply (This,[name,version]);//Inheritance Constructor
this.city = City;
House.prototype.housename = "Peninsula Garden";
}
House.prototype = new Createclass ("Cofco Real Estate", "two Period");//Get Ceateclass property, including prototype object, via new
Prototype properties for print functions
function Displayprototype (c)
{
for (var x in C.prototype)
{
document.write (x+ "<br/>");
}
}
Displayprototype (house);/output: Houseprice company Name version
Delete an object that is not a prototype
Delete house.prototype.name;//deletion
Delete house.prototype.version;//deletion
Displayprototype (house);/output: Houseprice Company
var customers = new House ("Peninsula Garden", "three period", "West Tooth Extraction");
for (Var t in customers)
{
if (typeof customers[t] = = "function")//judge is not functions
{
CUSTOMERS[T] ()//Execute
continue;//return this time, for the next cycle
}
document.write (t + ": \ T" + customers[t]+ "<br/>");
Output housename: Peninsula Garden Company:vanke Tai Mei Sha Peak Mansion selling 50,000,001 sets, best-selling price! Name: Peninsula Garden version: Three city: West tooth extraction
The inheritance is realized. Through the prototype.

Summary: This article and everyone to share here, there would have been namespaces to share, due to the relationship between learning time arrangement, JavaScript Grammar for everyone to share here!

Next time I'm going to share my programming with JavaScript customer service and advanced applications like jquery.

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.