Some exercises of this in JavaScript

Source: Internet
Author: User

<! DOCTYPE html>The //on method passes a parameter to the event listener's handler function event.target the this point inside the handler function//Event.target So the printed this is Event.target <div class= "Child" ></div>
$ ('. Child '). On (' click ', function () {///Console.log (this);//<div class= "Child" ></div>//})

//Event delegate, the first literally means to see you out, is to put the event to others to execute,//Lenovo to the above-mentioned event bubbling, is not thought of? Yes,//is to pass the event of the child element to the parent element in the form of bubbling. The //event delegate not only implements the same functionality, but also greatly reduces DOM operations.
//Monitor. Child's Click event under Father.$ ('. Father '). On (' click ', '. Child ', function () {//Console.log (this);//<div class= "Child" ></div>//})// $ ('. Child ') [0].on (' click ', function () {//Console.log (this);//<div class= "Child" ></div>//})
var app={init:function () {//init () This refers to the appthis. $father =$ ('. Father ');//Bind an attribute to the App object $fatherthis. $child =$ ('. Child ');//bind an attribute to an App object $childThis.bind ();//app.bind ()}, Bind:function () {var _this=this;//this refers to the app
//Monitor. Father passed the Event.target to This.sayhi when the click event was heard.//That is. Father Dom Object <div class= "father" ><div class= "Child" ></div></div>this. $father. On (' click ', This.sayhi);
//Overwrite _this.sayhello () to _this.sayhello.call (_this)//SayHello at the time of execution, the internal this point of the app object itselfthis. $child. On (' click ', Function () {_this.sayhello ();})
//If there is no bind (this), This.saybye will accept the event.target that is passed. Child's Dom object//But this is bound by the this and the rejected this is a consistent reference to the App objectthis. $child-On (' Click ', This.sayBye.bind (This)}, Sayhi:function () {console.log (' sayhi ', this)//<div class= " Father "><div class=" Child "></div></div>}, Sayhello:function () {console.log (' SayHello ', this)/ /app}, Saybye:function () {console.log (' Saybye ', this)//app}} app.init (); </script></body>

Some exercises 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.