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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:58:13+00:00 2026-06-07T01:58:13+00:00

I was reading John Resig’s article about JavaScript Getters and Setters when I found

  • 0

I was reading John Resig’s article about JavaScript Getters and Setters when I found this code:

function Field(val){
    this._value = val;
}
Field.prototype = {
    get value(){
        return this._value;
    },
    set value(val){
        this._value = val;
    }
};

I’ve tested and it works perfectly with all major browsers, except the damn IE, which gives me the SCRIPT1003: ':' expected error. After wondering for a while, I realized that this error happens because IE doesn’t recognize JavaScript Getters and Setters, so I thinks that get value and set value are a syntax error.

Is there any way to make this code cross-browser?

Thanks in advance

EDIT:

I also tried to check if the browser supports getters&setters:

if(window.__lookupSetter){
    Field.prototype = {
        get value(){
            return this._value;
        },
        set value(val){
            this._value = val;
        }
    };
}else{
    Field.prototype = {
        value: function(val){
            if(val)
                return this._value = val;
            return this._value;
        }
    };
}

But before executing the code, IE checks for syntax errors, and it wrongly finds these errors in the get and set lines.

  • 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-07T01:58:15+00:00Added an answer on June 7, 2026 at 1:58 am

    You can define properties without using new syntax with Object.defineProperty:

    function Field(val){
        this.value = val;
    }
    Object.defineProperty(Field.prototype, 'value', {
        get: function(){
            return this._value;
        },
        set: function(val){
            this._value = val;
        }
    });
    

    That way, the code won’t give a syntax error in older browsers.


    WRT to your edit, your fallback code:

    Field.prototype = {
        value: this._value
    };
    

    Will not work. this will point to the global object – window.


    The only way to truly use getters and setters cross browser is not to use them at all:

    Field.prototype = {
        getValue: function() {
            return this._value;
        },
        setValue: function(val) {
            this._value = val;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading John Resig's Secrets of JavaScript Ninja and saw this code: function
I'm reading the John Resig blog about the Strict Mode in javascript, but i
I m reading John Resig s Learning Advanced JavaScript http://ejohn.org/apps/learn/#10 and came across this
So, I was reading up on John Resig's blog , saw his micro-templating javascript
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
I am a newbie to java script and currently reading John Resig's Pro javascript
In this John Resig article , he's is dealing with a dictionary-sized list of
I've been doing some reading about two (relatively) new concepts in the Javascript language
I have just finished reading an article on MSDN by John Evdemon . He
I'm reading a simple xml file using matlab's xmlread internal function. <root> <ref> <requestor>John

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.