The usage example of the JS function This is an analysis of _javascript techniques

Source: Internet
Author: User

This example describes the use of the JS function this. Share to everyone for your reference. Specifically as follows:

When writing functions in JS, a lot of this is used. What this is, this is a keyword, is a pointer to the execution environment scope, also known as the context.
Let's say the function is a code block that is called repeatedly in the language.
In JS, the function is assigned to the object's properties, called the method
Such as:

var m={};
m.title= ' title ';
M.show=function () {
alert (this.title)
}
m.show ()

is to call the function as the object M method.
In this case, this point is the object of M.

Called function of direct function name:

var a=1212;
Function Show () {
alert (a)//1212
} show
()//1212

In the global context, global variables can be interpreted as window properties, and global functions are window methods
Look at the following example:

var m ={};
M.id= ' mmm ';
M.show=function () {
  alert (this.id);
}
var a={};
A.id= ' AAA ';
A.show=m.show;
A.show (); AAA

A.show=m.show;  Understand this sentence first, because the function is an object,
m.show=function () {
  alert (this.id)
}

This expression is equivalent to the a.show and m.show references pointing

function () {
  alert (this.id)
}

actually equals

A.show=function () {
  alert (this.id)
}

So when calling A.show (), this is pointing to a object,
And look at one of the chestnuts below.

var m ={};
M.id= ' mmm '
m.show=function () {
  alert (this.id)
}
var a={}
a.id= ' aaa '
a.show= function () {
  m.show ()
};
A.show (); Mmm

So calling A.show () is equivalent to calling the M.show () method, so this. is the M object.

Look at one of the following examples, at the beginning still not very understand

var color= ' red ';
var app = {};
App.color= "Green";
App.paint=function (node) {
node.style.color=this.color;
  alert (This.color);
}
function Findnode (callback) {
  var btn =document.queryselector ('. btn ');
  Callback (BTN);//Pass in,
}
Findnode (app.paint);
alert (This.color); Red, not green.

When a function argument is passed, it is passed by value, not by reference

So Findnode (App.paint), when it came in, was actually

function (node) {
  node.style.color=this.color;
  alert (This.color);
}

, and because Findnode is defined globally, this points to window OR UNDEFINED;

About arguments, passing the past by value

Function Show (a) {
  alert (a)
}

When the parameter is a basic data type, it's understandable.

var b=10;
Show (b)//alert (10);

As for the object

var c ={};
C.prop=true;
var showprop=function (obj) {
obj.prop=false
}
Showprop (c);//c.prop = False

Some people think the above example is by reference to the argument
In fact, above or by the value of the argument, Showprop (c) into the function, C is actually equivalent to a reference, the function of Obj.prop=false, the equivalent of the reference to the object of {Prop:false}

Let's look at one of the following examples

var c ={};
C.prop=true;
var showprop=function (obj) {
  obj = new Object ();
  Obj.prop=false return
  obj;
}
Showprop (c);
alert (C.prop); True

Here obviously the incoming obj modified, if the function according to the reference parameters, the changes in the function will be reflected to the external

I hope this article will help you with your JavaScript programming.

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.