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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:20:17+00:00 2026-06-08T05:20:17+00:00

In JavaScript, I simulate classes like this: function MyClass() {} // Constructor MyClass.prototype.myField =

  • 0

In JavaScript, I simulate classes like this:

function MyClass() {} // Constructor

MyClass.prototype.myField = "foo";

I thought I’d make the code more readable and save keystrokes by using the with keyword:

function MyClass() {} // Constructor

with (MyClass.prototype)
{
    myField = "foo";

    myOtherField = "bar";

    myMethod = // etc
}

But if I then create an instance and try to access any of those fields, they’re undefined. However, if I do with (MyClass.prototype) and then try to read values from it, that works – it’s only when I try to change them or create new fields that it fails. I’m assuming this is because with (myObject) scopes you to a clone of myObject, not an actual reference to the original instance.

So, is there any way to get around this? Any way to produce the results I want, with or with out with? Or am I doomed to writing out MyClass.prototype over and over again?

  • 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-08T05:20:21+00:00Added an answer on June 8, 2026 at 5:20 am

    “if I then create an instance and try to access any of those fields, they’re undefined“

    You can’t create new fields that way.

    “if I do with (MyClass.prototype) and then try to read values from it, that works”

    This is because you can read and update existing fields.

    “I’m assuming this is because with (myObject) scopes you to a clone of myObject, not an actual reference to the original instance. “

    No, it’s an actual reference to the object.


    This sort of confusion is part of the reason why people avoid with.


    “So, is there any way to get around this? Any way to produce the results I want, with or with out with? Or am I doomed to writing out MyClass.prototype over and over again?”

    Just use a variable.

    var p = MyClass.prototype;
    
    p.myField = "foo";
    p.myOtherField = "bar";
    p.myMethod = "etc";
    

    You can also overwrite the existing prototype object, allowing object literal notation…

    function MyClass() {}
    
    MyClass.prototype = {
        myField: "foo",
        myOtherField: "bar",
        myMethod: "etc"
    };
    

    You can also add a constructor: MyClass if you want that.

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

Sidebar

Related Questions

Can I Simulate this Code With JQUERY or Javascript? IEnumerable<TextBox> Textboxes = (from c
I had written this code to simulate OOP inheritance and calling baseclass in javascript
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
I have this small piece of code and i want to to simulate the
I'm trying to simulate the Matrix code rain with the canvas element and javascript.
I have a JavaScript object that does something like this - using a closure
I have this javascript: $('.foto').filter(function(index) { return index == Math.floor(Math.random() * 8) + 1;
I have a link like this: <a href=www.example.com> I would like to simulate the
We would like to simulate Imagemagick functionality, but only in Javascript. Do any libraries

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.