Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:33:06+00:00 2026-06-03T00:33:06+00:00

Take this class: var MyClass = new Class({ Implements: [Events, Options], initialize: function() {

  • 0

Take this class:

var MyClass = new Class({
    Implements: [Events, Options],
    initialize: function() {

        this.a = 1;

    },

    myMethod: function() {

        var mc = new differentClass({
            events: {
                onClick: function() {

                    console.log(this.a); // undefined (should be 1, 2, 3 etc)

                    this.a ++;


                }
            }
        });    

    }
});

How do I keep the value of this.a? I am basically trying to draw a line (using canvas) from the last point to the co-ordinates just clicked.

[EDIT]

I dont want to bind this as it’s bad apparently and it will over-ride the differentClass options.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:33:09+00:00Added an answer on June 3, 2026 at 12:33 am

    several patterns are available.

    decorator via .bind()

    var mc = new differentClass({
        events: {
            click: function() {
                 console.log(this.a);
                 this.a ++;
            }.bind(this) // binds the scope of the function to the upper scope (myclass)
        }
    });    
    

    keeping a reference.

    var self = this; // reference parent instance of myClass
    var mc = new differentClass({
        events: {
            click: function() {
                 console.log(self.a); 
                 self.a ++;
            }
        }
    });    
    

    pointing to a myClass method that can deal with it:

    handleClick: function() {
        this.a++;
    },
    myMethod: function() {
        var mc = new differentClass({
            events: {
                click: this.handleClick.bind(this)
            }
        });    
    }
    

    the 2-nd one – by storing a reference is preferred due to the smaller footprint and universal support whereas .bind is not available in every browser and needs to be shimmed as well as the extra time to curry the function on execution.

    self is what you will find in mootools-core itself when possible.

    if performance is not at risk, method 3 can probably offer the best readability and code structure. the arguments to the method will remain what the click handler passes, i.e. event and event.target will be the handler.

    in pattern #2 with self, this will point to the click handler within the anonymous function (or to the other class, for example), which may be useful as well – rebinding context can be a pain in the neck

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take this code: public class MyClass { private final Object _lock = new Object();
I find myself lacking knowledge around the below. Take this class: public class MyClass
Take this code: class Register(var value:Int = 0) { def getZeroFlag() : Boolean =
Take this simple example: class Program { static void Main(string[] args) { var windsorContainer
Take this class as example: public class Category : PersistentObject<int> { public virtual string
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
How do i create aliases in c# Take this scenario class CommandMessages { string
I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
take this simple code: class A{ public: virtual void foo() = 0; void x(){
Take this mapping file <class name=com.data.StudentData table=gr_student> <id name=id> <generator class=identity /> </id> <property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.