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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:29:20+00:00 2026-06-15T15:29:20+00:00

This may turn out to be a very basic question as I am a

  • 0

This may turn out to be a very basic question as I am a newbie. I am creating an object from a constructor. I want one of the properties of object to be linked to a variable. So if variable value changes, the property’s value should also change.

Example: I am working on kineticjs and am creating an object from constructor Rect. I want the value of the property draggable to dynamically change to the value of optvar whenever optvar changes.

Var optvar="true";

rect = new Kinetic.Rect({ 
    x: 22, 
    y: 7, 
    width: 0, 
    height: 0,
     fill: 'red', 
    stroke: 'black', 
    strokeWidth: 4, 
    draggable: optvar    
});

optvar="false"; // I want value if rect.draggable to be equal to false
  • 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-15T15:29:21+00:00Added an answer on June 15, 2026 at 3:29 pm

    This is not a basic question. 🙂

    As of ES5, it’s possible to define properties with setters and getters via Object.defineProperty (and Object.defineProperties). This feature of ES5 is quite broadly supported in modern browsers, but older browsers (such as IE8 and earlier) do not have it.

    Using a getter, it’s possible to do this:

    var optvar=true;
    
    rect = new Kinetic.Rect({ 
        x: 22, 
        y: 7, 
        width: 0, 
        height: 0,
         fill: 'red', 
        stroke: 'black', 
        strokeWidth: 4
    });
    Object.defineProperty(rect, "draggable", {
        enumerable: true,
        get:        function() {
            return optvar;
        }
    });
    

    That creates a property on rect that, when retrieved, returns the current value of optvar. It works because the getter function is a closure over the optvar variable (more about closures in my blog: Closures are not complicated.)

    Of course, whether this works correctly with Kinetic.Rect will depend on how Kinetic.Rect is implemented.

    The property created above is enumerable [shows up in for..in loops like the others do], but is not writable. If you wanted it to be writable:

    Object.defineProperty(rect, "draggable", {
        enumerable: true,
        get:        function() {
            return optvar;
        },
        writable:   true,
        set:        function(value) {
            optvar = value;
        }
    });
    

    From your comment on the question:

    I have multiple objects rect (all created dynamically). I want all objects’ property to be linked to one variable.

    The above mechanism can be used to do that, because of course, you can have getters for all of your rects:

    var optvar = true;
    var rect;
    var rects = [];
    
    while (rects.length < 10) {    
        rect = new Kinetic.Rect({ 
            x: 22, 
            y: 7, 
            width: 0, 
            height: 0,
            fill: 'red', 
            stroke: 'black', 
            strokeWidth: 4
        });
        Object.defineProperty(rect, "draggable", {
            enumerable: true,
            get:        getDraggable
        });
    
        rects.push(rect);
    }
    function getDraggable() {
        return optvar;
    }
    

    Now, all 10 of those rects will be draggable, or not, based on optvar.

    (Note: I’m assuming all of this code is in a function somewhere, so we’re not creating global variables.)

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

Sidebar

Related Questions

This may turn out to be a simple question with a very complex answer,
this may sound a newbie question anyway Im new to GCD, I'm creating and
I am sorry ahead of time for how this question may turn out. I
This may turn out to be an embarrassingly stupid question, but better than potentially
This may turn out to be 2 questions in one, but I believe solving
The answer to this question may turn out to be, Don't use typed DataSets
This may sound like a very generic question but here it goes. I have
this may sound pretty straight forward, but still I want to post this question
I'm pretty new to Umbraco, so my question may turn out to be pretty
This may be a stupid question but I have a code with the following

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.