The with, this usage summary in JavaScript

Source: Internet
Author: User
Tags class definition cos sin
the WITH, this usage summary in JavaScriptThe WITH statement specifies the default object for one or a set of statements.

Usage: With (< OBJECT >) < statement >;

The With statement is typically used to shorten the amount of code that must be written in a particular case. In the following example, note the repeated use of Math:

x = Math.Cos (3 * Math.PI) + Math.sin (MATH.LN10);
y = Math.tan (MATH.E);

When you use the WITH statement, the code becomes shorter and easier to read:

With (Math) {
x = cos (3 * PI) + sin (LN10);
y = tan (E);
}

The This object returns the current object. In different places, this represents a different object. If this is used in JavaScript in the main program (not in any function, not in any event handler), it represents the Window object, and if this is used in a with statement block, it represents the object specified with, if it is in an event handler Using this, it represents the object where the event occurred.

A commonly used this usage:

<script>
...
function Check (formobj) {
...
}
...
</script>

<body ...>
...
<form ...>
...
<input type= "text" ... onchange= "check (this.form)" >
...
</form>
...
</body>

This usage is often used to immediately detect the validity of form input.
---------------------------------------------
Usage of this in javascript:
1.<div onclick= "//Can be used inside this" >division element</div> this point to div
2. <div id= "Elmtdiv" >division element</div>
<script language= "JavaScript" >
var div = document.getElementById (' Elmtdiv ');
Div.attachevent (' onclick ', EventHandler); Attachevent to bind a div's onclick event to a method
function EventHandler ()
{
Use this here
}
</script> this is to point to the Window object, to reference the Div object this.event.srcElement;
3. Use the This keyword in the event handler function in DHTML mode:
<div id= "Elmtdiv" >division element</div>
<script language= "JavaScript" >
var div = document.getElementById (' Elmtdiv ');
Div.onclick = function ()
{
Use this here
};
</script> methods above, but here's this point to div
4. Use this keyword in class definition:
function Jsclass ()
{
var myname = ' Jsclass ';
This.m_name = ' Jsclass ';
}
JSClass.prototype.ToString = function ()
{
Alert (myname + ', ' + this.m_name);
};
var JC = new Jsclass ();
Jc. ToString ()//This is the use of this in the JavaScript mock class definition, which is very familiar with the situation in other OO languages. But this requires that member properties and methods must be referenced using the This keyword, and running the program above will be told MyName undefined.
5. Add the This keyword in the prototype method to the script engine internal object:
Function.prototype.GetName = function ()
{
var fnname = this.tostring ();
FnName = fnname.substr (0, Fnname.indexof ('));
FnName = Fnname.replace (/^function/, ");
Return Fnname.replace (/(^\s+) | ( \s+$)/g, "");
}
function foo () {}
Alert (foo.    GetName ()); This here refers to an instance of the class being added to the prototype, somewhat similar to the class definition in 4, nothing too special.
Turn from: http://millton0518.iteye.com/blog/1889519

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.