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

  • Home
  • SEARCH
  • 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 8594551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:13:32+00:00 2026-06-12T00:13:32+00:00

In JavaScript you can define getters and setters by using Object.defineProperty() . I am

  • 0

In JavaScript you can define getters and setters by using Object.defineProperty(). I am wondering if it is possible to extend or use the prototype of this to extend its functionality. Here is an example:

I start off a variable called color:

Object.defineProperty(window, 'color', {
    get: function() {
        return [_color.red,_color.green,_color.blue,_color.alpha];
    },
    set: function(val) {
        _color.red = val[0];
        _color.green = val[1];
        _color.blue = val[2];
        _color.alpha = val[3];
    }
});

This allows for me to both set and get the color by passing rgba arrays to and from the variables. Here is how I would use this:

color = [0,127,255,255];
alert(color);
//alerts [0,127,255,255]

Now I also want to be able to edit these variables through accessing each variable individually.

color.r = 255;
alert(color);
//alerts [255,127,255,255]

I am at an impasse at this moment because I don’t know what I can do to create this. I would think either using color.prototype.r or something similar would work, but I can’t get it to. Is it possible to do something like this?

  • 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-12T00:13:33+00:00Added an answer on June 12, 2026 at 12:13 am

    You have to create an interim object extended from array to do it specifically.

    Here you go:

    var __color = {red:0,green:0,blue:0,alpha:1};
    function createGetMethod(prop){
        return function(){return __color[prop]};
    }
    function createSetMethod(prop){
        return function(val){__color[prop]=val};
    }
    function createDescritorProperty(prop){
        return {
            get: createGetMethod(prop),
            set: createSetMethod(prop)
        }
    }
    
    var _color_descriptor = {
        red:createDescritorProperty('red'),
        green:createDescritorProperty('green'),
        blue:createDescritorProperty('blue'),
        alpha:createDescritorProperty('alpha'),
        length:{
            value:4,
            writable:false
        }
    }
    //setup numbered values
    _color_descriptor[0]=createDescritorProperty('red');
    _color_descriptor[1]=createDescritorProperty('green');
    _color_descriptor[2]=createDescritorProperty('blue');
    _color_descriptor[3]=createDescritorProperty('alpha');
    
    var _color = Object.create(Array.prototype, _color_descriptor); //extends array
    
    
    Object.defineProperty(window, 'color', {
        get: function() {
            return _color;
        },
        set: function(val) {
            _color.red = val[0];
            _color.green = val[1];
            _color.blue = val[2];
            _color.alpha = val[3];
        }
    });
    
    window.color = [255,255,255,0];
    alert(window.color);
    window.color.red=10;
    alert(window.color);
    

    Hope it works! 😀

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

Sidebar

Related Questions

In JavaScript, you can define an object like this: var d = {1: 'test'};
When key name starts with digit, in javascript we can define array-like object like
In Javascript you can define an object with properties a and b like so:
Its possible with TinyMCE to define custom URL converter logic as defined in this
In Javascript we can use 'set' and 'get' to define properties, but is there
i have a object in javascript and some already defined functions. but how can
In Javascript i have read that we can define our own value for undefined
While writing javascript, one can define a method in 3 different ways. 1] A
This is a noob question. I am trying to use node.js as a JavaScript
Can someone explain what is wrong with this JavaScript example, and how to fix

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.