Js~this's Trap

Source: Internet
Author: User

In JS, the current object is generally represented by this, in jquery, the current object is expressed in $ (this), these are the most basic knowledge, nothing to say, but I would say, when this appears at a certain depth, it means you have to understand, It refers to the current object that is closest to the current element, not to another object.

This example

For example, in a function-represented class, it may have its own method inside, and this in the method represents the current method object, not the class object at the function level, the code is as follows

var cart=function() {var self=this;   This is the current function object self. add=function() {var insertobj=this;   This is the current Add method object }}
$ (This) example

For jquery is also a, such as in a click event, there is a $.post method, at this time, the $.post method of $ (this) and the method of $ (this) is two meaning, so if you want to use the Click Object in the $.post method of this, Generally need to declare outside the $.post method, the code is as follows

// Delete        $ (". Deletebtn"). Click (function  () {            var = $ (this);  the current Click event Source Object            $.post ("/course/delcourse?id=" + $(thisfunction  ( Data) {                if (Data.code = = 1) {                    self.closest ("Div.titdefault"). Remove ();            });        });

Js~this's Trap

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.