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 8783555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:51:06+00:00 2026-06-13T20:51:06+00:00

My code is very simple. Ans to me it should work. var preview =

  • 0

My code is very simple. Ans to me it should work.

var preview = WinJS.Class.define(
   function (el, options) {
       el.winControl = this;
       this.el = el;

       this.textarea = d.getElementById('preview-input');
       this.preview = d.getElementById('preview-text');
       this.form = d.getElementById('perview-form');

       this.preview.addEventListener('click', this.click, false);
       //WinJS.Utilities.query("button", this.form)
       //this.preview.addEventListener('', this.save, false);

   },

   {
       click: function (e) {
           this.form.style('display', 'block');
       }
   }
);

WinJS.Namespace.define('RegCtrl', { preview: preview });

But when click occurs this.form seems to be undefined of null. Why? I do not want to initialize objects in every method of the class.

New tests

I made additional test very small

var preview = WinJS.Class.define(
   function (el, options) {
       var test = 1;
       this.test = 1;
       this.test1();
   },

   {
       test1: function () {
           console.log(this.form, test);
       }
   }

);
WinJS.Namespace.define(‘RegCtrl’, { preview: preview });

This test fails on line this.test1();. What I think now that this class is called RegCtrl.preview() rather than new RegCtrl.preview().

How do I shek inside the function that this called as new but not a simple function?

  • 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-13T20:51:07+00:00Added an answer on June 13, 2026 at 8:51 pm

    The other answers aren’t explaining what’s going on, and as such are giving incorrect advice.

    JavaScript has first-class function objects – you can pass them around as values. That’s exactly what you’re doing when you set up this callback:

    this.preview.addEventListener('click', this.click, false);
    

    You’re taking the contents of the this.click property, which happens to be a function, and handing it to the addEventListener function to do whatever it wants with it.

    I was very specific about terminology there – note I specifically said function, not method. JavaScript doesn’t really have a method construct, it just has methods as properties on an object.

    So where does the “this” member come from? It’s determined at the caller – the object you use on the left side of the ‘.’ is the one that becomes the value of this. For example,

    function exampleFunc() { console.log("this.myName = " + this.myName); }
    
    var a = { myName: "Chris", doSomething: exampleFunc };
    var b = { myName: "Bob", doSomething: exampleFunc };
    

    Note I’ve assigned the exact same function to the doSomething properties. What what happens:

    a.doSomething(); // Outputs "this.myName = Chris"
    b.doSomething(); // Outputs "this.myName = Bob"
    

    The exact same function object, called through two different objects, has a different this pointer.

    exampleFunc is a global function, let’s call it:

    exampleFunc() // Outputs "this.myName = undefined"
    

    So where’d the undefined come from? In a global function, “this” is set to window (the global scope), which didn’t have the myName property defined. Which also means that you could do this instead:

    myName = "Global Name"; // note, no var - we want this global
    exampleFunc(); // Outputs "this.myName = Global Name"
    

    Ok, so what’s going on with the original question? Basically, you’ve passed the function this.click to be the callback, but you haven’t passed the “this” pointer that you want it called through. Actually, addEventListener doesn’t have a way to pass the this pointer. As a result, when the function is invoked this is not pointing at your object. I don’t remember off the top of my head what it’s pointing at – it’s either window or the element that was clicked on, check the DOM documentation to verify.

    To get it to call the right function with the right context (context = the correct “this”), the traditional approach is to use a closure. Capture “this” in a variable, then pass in an anonymous function that calls your actual callback with the right this pointer. The code looks like this:

    var preview = WinJS.Class.define(
       function (el, options) {
    
           // Capture your current this pointer in a global variable
           // Using "that" as the name comes from JavaScript: The Good Parts book
           var that = this;
    
           el.winControl = this;
           this.el = el;
    
           this.textarea = d.getElementById('preview-input');
           this.preview = d.getElementById('preview-text');
           this.form = d.getElementById('perview-form');
    
           // Note what gets passed instead of this.click:
           this.preview.addEventListener('click', 
               function (e) {
                   // NOTE: Calling through "that": "this" isn't pointing to the right object anymore
                   // Calling through "that" resets "this" inside the call to click
                   that.click(e);
               }, false);    
       },
    
       {
           click: function (e) {
               this.form.style('display', 'block');
           }
       }
    );
    

    This is a common enough pattern that ECMAScript 5 has a utility function to build these wrappers for you – function.bind. Do this:

           this.preview.addEventListener('click', 
               this.click.bind(this),
               false);    
    

    The construct this.click.bind(this) will construct a new function that, when called, will set the “this” reference to whatever you passed (in this case “this”), and then invoke the function you called it on.

    Yes, there are a lot of different values for “this” floating around. Keeping track of what “this” is pointing at is an important part of mastering JavaScript programming.

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

Sidebar

Related Questions

I have got the following very simple code: function init() { var articleTabs =
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout
In this very simple code example, messages get lost every once in a while.
I have a very simple code: package mygame; public class RunGame { public static
This is a very simple code in place of a bigger problem, but I'm
My code is very simple, just create a empty table. This is my code:
Here is code of very simple expression evaluator using IronRuby public class BasicRubyExpressionEvaluator {
The code is very simple. But this is the first thing off this kind
This should be very simple for someone who knows regex. I don't need anything
The below code is very simple. I have a jQuery autocomplete bound to an

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.