Some of the quirks of this in JavaScript

Source: Internet
Author: User
Tags tagname

 <!--JavaScript Pseudo-protocol and inline events are different for this point -    <ahref="#"onclick= "alert (this.tagname);">Click Me</a><!--Eject a -    <ahref= "Javascript:alert (this.tagname);">Click Me</a><!--Eject undefined -    <ahref= "Javascript:alert (This==window);">Click Me</a><!--Popup True -    <inputID= "BTN"type= "button"value= "This demo"name= "button"/>

<script type= "Text/javascript" >varname = ' Somebody '; varAngela ={name:' Angela ', say:function() {alert ("I ' m" + This. Name);        }        }; varBTN = document.getElementById (' btn ')); //settimeout and SetInterval will also change the point of this        /*Angela.say ();//i ' M Angela SetTimeout (Angela.say, n);//i ' m Somebody setinterval (Angela.say, //i ' m Somebody*/        //On ... will also change the point of this        /*Angela.say ();//i ' m angela Btn.onclick = angela.say;//i ' m button*/        //anonymous function adjustment this pointSetTimeout (function() {Angela.say ();}, 1000);//I ' m AngelaSetInterval (function() {Angela.say ();}, 1000)//I ' m AngelaBtn.onclick =function() {Angela.say ();};//I ' m AngelaSetTimeout (function() {Alert ( This= = window); }, 1000);//trueBtn.onclick =function() {Alert ( This= = BTN); }//true        //Call and apply adjust the point of thisAngela.say.call (BTN);//I ' m buttonSetTimeout (function() {Angela.say.call (BTN);}, 1000);//I ' m buttonSetTimeout (function() {angela.say.apply (BTN);}, 1000);//I ' m buttonBtn.onclick =function() {angela.say.apply (BTN);}//I ' m button        //saves the object that this is pointing to a variable        varname = ' Migo '; varMydemo ={name:' Angela ', say:function() {alert ("I ' m" + This. Name); }, Init:function () {                varthat = This; document.getElementById (' btn '). onclick =function() {That.say ();//I ' m Angela                     This. Say ();//Here's an error This.say is not function                }            }        }; </script>

Some of the quirks of this in JavaScript

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.