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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:28:48+00:00 2026-05-24T11:28:48+00:00

How do I emulate PHP-style __get() and __set() magic getter/setters in JavaScript? A lot

  • 0

How do I emulate PHP-style __get() and __set() magic getter/setters in JavaScript? A lot of people say that this is currently impossible. I am almost certain that it is possible because projects like nowjs (http://nowjs.com) do something like this.

I know that you can utilize get and set, but these don’t work when you’re not sure what the property name will be. For example, what if you wanted an event handler to execute when a new property is created?

Example of what I’d want to do:

var obj = {};
notify(obj, function(key, value) {
   //key is now the name of the property being set.
   //value is the value of the property about to be set
   console.log("setting " + key + " to " + value);
});
obj.foo = 2; //prints "setting foo to 2"
obj.bar = {a: 2}; //prints "setting bar to [Object]"
//Notice that notify() worked even though 'foo' and 'bar' weren't even defined yet!

(The question is similar to the following questions:

  • Is there a way to monitor changes to an object?
  • JavaScript getter for all properties

)

EDIT: It looks like this feature is called “dynamic proxies” and should appear in the ECMAScript “Harmony” standard (probably ES6). You can read more here. A new ‘Proxy’ Object is introduced with a couple methods (I.e. Create() and createFunction() ).

One could do this:

//Constructing an object proxy (proto is optional)
var proxy = Proxy.create(handler, proto);
proxy.foo = 2; //Triggers 'set' function in the handler (read about it)

Bottom line here: it doesn’t work in most browsers, but an implementation is available for Node.js: node-proxy.

  • 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-24T11:28:49+00:00Added an answer on May 24, 2026 at 11:28 am

    Looking through the nowjs source code, I believe they do this by continuously monitoring the now object and pushing changes between client and server whenever they are detected. I admit I haven’t fully grokked their code yet, however.

    In a browser, this would be done with some fun setInterval hacks.

    EDIT: yes, that is indeed what they do: line 368 of the client now.js. They do some more tricks so that once a new property is detected, future access to it is caught by getters and setters, but those modifications are only made every 1000 ms in a setTimeout.

    Another piece of evidence that this is impossible in current JavaScript is that the proxies proposal for ECMAScript Harmony is designed explicitly to enable such scenarios, implying very strongly that they can’t be done currently. Recent Mozilla browsers have a prototype proxies implementation, if perhaps that’s enough. And apparently V8 is working to add support, which could be enough depending on what version of V8 Node is using these days.

    EDIT2: oh cool, on the server side apparently nowjs does use proxies! Which likely means they are mature enough in Node for your usage. See what they do at https://github.com/Flotype/now/blob/master/lib/proxy.js. Or just do var Proxy = require("nodejs-proxy") and hope they follow the spec so you can take advantage of the documentation from MDC and elsewhere.

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

Sidebar

Related Questions

Is it possible to emulate a C union structure using Scala? In this case
We are currently running an ajax auto complete script through mySQL, as such: <?php
We currently have a quote page which lists all existing quotes that we would
I currently have a large form that gets sent to our payment authorizer (done
I'm using PHP 5.3 and SimpleTest, but more general answers are welcome. Each plug-in
Im trying to make a function to run it later. In php 5.3.2-1 it
What calls best emulate pread/pwrite in MSVC 10?
I need solution to emulate fixed position, but relative parent div, not whole viewport.
I'm trying to emulate Apple's Slide to Unlock feature in my application. I get
Is it possible to emulate boot from flash memory using qemu-system-arm ? (Using Integrator/CP

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.