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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:10:54+00:00 2026-05-24T03:10:54+00:00

In Firefox , I’ve got several objects that I need to trigger an event

  • 0

In Firefox, I’ve got several objects that I need to trigger an event when a particular property of each is changed. I’m using object.watch(), however when I return the value of the property that was changed using “this”, it returns the old value the first time, and “undefined” the second and subsequent times:

var myObject = {
        "aProperty": 1
    };

function propChanged(prop) {
    alert(prop);
}

myObject.watch("aProperty", function () {
    propChanged(this.aProperty);
});

myObject.aProperty = 2;//alerts "1"
myObject.aProperty = 3;//alerts "undefined"

The reason I can’t just say alert(myObject.aProperty) is because this is meant to be a dynamic code that will apply the event handler to several, possibly unknown objects.

I’m just unsure exactly how to dynamically get the new value of the property using the watch method. I’m setting up a prototype for IE for this, so I’m not worried about it not working there. I just need to understand “this” and how it applies to the watch method’s owner.

Edit>>

Here’s the new code I’m using for cross browser, including the IE et al prototype:

var myObject = {};

if (!Object.prototype.watch) {
    Object.prototype.watch = function (prop, handler) {
        var oldval = this[prop], newval = oldval,
        getter = function () {
            return newval;
        },
        setter = function (val) {
            oldval = newval;
            return newval = handler.call(this, prop, oldval, val);
        };
        if (delete this[prop]) { // can't watch constants
            if (Object.defineProperty) // ECMAScript 5
                Object.defineProperty(this, prop, {
                    get: getter,
                    set: setter
                });
            else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
                Object.prototype.__defineGetter__.call(this, prop, getter);
                Object.prototype.__defineSetter__.call(this, prop, setter);
            }
        }
    };
}

if (!Object.prototype.unwatch) {
    Object.prototype.unwatch = function (prop) {
        var val = this[prop];
        delete this[prop]; // remove accessors
        this[prop] = val;
    };
}


function propChanged(t, p, o, n) {
    alert(o);
}

Object.defineProperty(myObject, "aProperty", {value: 2,
    writable: true,
    enumerable: true,
    configurable: true});

myObject.watch("aProperty", propChanged);

myObject.aProperty = 3; //alerts 3
myObject.aProperty = 4;  //alerts 4 (n is undefined in propChanged?
  • 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-05-24T03:10:55+00:00Added an answer on May 24, 2026 at 3:10 am

    You need to return the value you want the property to have from the function you pass to watch.

    myObject.watch("aProperty", function (prop, oldval, newval) {
        propChanged(newVal);
        return newVal;
    });
    

    should do it.

    See the MDN docs for a full detail of the function but the relevant bit is

    Watches for assignment to a property named prop in this object, calling handler(prop, oldval, newval) whenever prop is set and storing the return value in that property. A watchpoint can filter (or nullify) the value assignment, by returning a modified newval (or by returning oldval).

    EDIT

    Your edited code might work better this way

    Object.prototype.watch = function (prop, handler) {
        var fromPrototype = !Object.hasOwnProperty.call(this, prop),
        val = this[prop],
        getter = function () {
            return fromPrototype ? Object.getPrototypeOf(this)[prop] : val;
        },
        setter = function (newval) {
            fromPrototype = false;
            return val = handler.call(this, prop, val, newval);
        };
        if (delete this[prop]) { // can't watch constants
            if (Object.defineProperty) { // ECMAScript 5
                Object.defineProperty(this, prop, {
                    get: getter,
                    set: setter,
                    configurable: true,
                    enumerable: true
                });
            } else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
                Object.prototype.__defineGetter__.call(this, prop, getter);
                Object.prototype.__defineSetter__.call(this, prop, setter);
            }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firefox 3.5 does not allow creating java OBJECT tag with Javascript (document.write)? this technique
Firefox recently blacklisted the plugin that allows xbaps to work in firefox This is
Firefox 3 came with a new allocator: jemalloc . I have heard at several
my firefox extension has an object myExt . myExt = { request: function(){ //adds
Firefox has an Xpather extension that allows one to inspect html and extract xpaths.
Firefox stores whole story in that file and what I want to do is
Using Firefox, working on a Firefox extension, I continually get a javascript warning: reference
Firefox displays correctly that the red Search Listing button is inline with the text
What Firefox add-ons do you use that are useful for programmers?
Firefox and Opera seem to put articles after each other while Chrome gives a

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.