Understand the function of JavaScript

Source: Internet
Author: User

The most distinctive and confusing function in JavaScript is one, and here's a look at common operations:

function doit () {...} Doit ();

Functions in JavaScript we can use it as a method Lanxi County Quwan Home Photography

var obj=new Object (); Obj.say=function () {...} Obj.say ();

And the function is actually an object (that is, an instance of the function type)

function result (NUM1, num2) {           return num1 + num2;} var result = new Function ("Num1", "num2", "return num1+num2");

The above execution effect is the same, and function result can also be written like this (that is, functional expressions)

var result=function (num1,num2) {return num1+num2;}

The only difference between the two is that the function is a priority, and the expression is the execution of the code, and there is an array-like arguments object inside each function, and the function executes the dynamic parameters, namely:

function result () {return arguments[0]+arguments[1];} Result (n);

Arguments are often used for dynamic transfer of parameters.

Since the function is an object, it should also be a specific attribute.

function person () {...} Person.name= "xxxx";p erson.say=function () {alert (this.name);} Person.say ();   Alert ("XXXX")

We can also think of it as a class, and the function body is equivalent to a constructor function

function person (nm) {this.name=nm;this.say=function () {alert (nm); alert (this.name);}} var p1=new person ("ygm1");p 1.say (); Alert ygm1 ygm1var p2=new person ("ygm2");p 2.say (); Alert YGM2 YGM2

Note that this is to be used this.name because this represents the current object, if the direct alert (name) is the property of the Window object, and the parameter nm passed in the method say can be used directly, in fact, this involves the scope chain, Each function body is a scope, the subdomain can access the properties of the parent domain, and the other is not (in fact, can be taken, design to the closure of some knowledge, here do not explain ...)

Each class can have some static properties or methods compared to some of the other OO languages, and JavaScript simulates it by prototyping to share its properties with each object.

function person (num) {...} Person.prototype.name = "YGM"; alert (new Person (). name);

However, the static method of Oo language is called by the class, cannot instantiate itself, in JavaScript because of its particularity exactly opposite.

Note here that the Name property of the Alertperson, if the function body does not find the name will be found in the prototype, if found will mask the name of the prototype directly return its value.

In fact, every time a function is created, a prototype object is created, and the prototype object is referenced from object, so object is the base class for all objects.

We can rewrite the prototype object person.prototype=new Parentperson ();

The person's prototype object points to the Parentperson object, and the Parentperson object points to its own prototype object ..., which forms the prototype chain ...

Understand the function of JavaScript

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.