Precautions for use of this in AVALONJS

Source: Internet
Author: User

The this pointer is typically a variable that exists based on the hosting environment, typically pointing to the host object, whereas for the MVVM framework, you need to create an operation on your own object in the VM, and you can use the method defined in the VM in V (view), which helps to understand:

JS model var vm = Avalon.define ({$id: ' VmID ', $getData: function () {console.log (this);//The point of the pointer is not determined by the location of the call) a Valon.ready (function () {VM. $getData ();}) DOM structure <div ms-controller= "VmID" > <a href= "javascript:void (0);" ms-click= "$getData ()" > Get Data </a> </div>

In this way, the VM object is used to print the method in the VM model, and the DOM node object is printed for the model object method call used in the DOM node, so be careful when using the this pointer in the MVVM framework.

The solution could be to redefine a method instead of directly using the $getdata () method in the VM object:

JS model var vm = Avalon.define ({$id: ' VmID ', $getData: function () {console.log (this); The pointer is indeterminate}, $jump: function () {console.log (this);  The this pointer points to the DOM node object vm. $getData ();  VM pointer to model object}}) Avalon.ready (function () {VM. $getData ();}) DOM structure <div ms-controller= "VmID" > <a href= "javascript:void (0);" ms-click= "$jump ()" > Get Data </a></ Div>

Precautions for use of this in AVALONJS

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.