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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:37:47+00:00 2026-05-23T17:37:47+00:00

I have this class where I have a private property and a public method

  • 0

I have this class where I have a private property and a public method for access:

Person = function () {
    this.Name = "asd";

    var _public = new Object();
    _public.Name = function (value) {
        if (value == undefined) { //Get
            return this.Name
        } else {
            this.Name = value; //Set
        }
    };

    return _public;
};

I want to force the context in _public.Name for access a this.Name.

I know the technique of closure, but I want to see if I can force a context.

I found a technique to do it, extend object Function:

Function.prototype.setScope = function (scope) {
    var f = this;

    return function () {
        f().apply(scope);
    }
}

And my class becomes:

Person = function () {
    this.Name = "asd";

    var _public = new Object();
    _public.Name = function (value) {
        if (value == undefined) {
            return this.Name
        } else {
            this.Name = value;
        }
    }.setScope(this);

    return _public;
};

So I can force correctly the context, but I can not pass value and can not, however, return this.Name.

  • 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-23T17:37:48+00:00Added an answer on May 23, 2026 at 5:37 pm

    Not

    f().apply(scope);
    

    just

    f.apply(scope);
    

    (No () after f.) You want to use the apply function on the function f object, not call the function f and access apply on its return value.

    To also pass on the arguments that your function in setScope receives, add this:

    f.apply(scope, arguments);
    

    arguments is an implicit argument to all functions, which is a pseudo-array of the actual arguments passed to the function at runtime. apply accepts any array-like thing as its second parameter to specify the arguments to use when calling the underlying function.

    I’d also have it return the return value:

    return f.apply(scope, arguments);
    

    So setScope becomes:

    Function.prototype.setScope = function (scope) {
        var f = this;
    
        return function () {
            return f.apply(scope, arguments);
        }
    }
    

    Live example

    Note that the usual name for this function, and the name it has in the new ECMAScript5 standard, is bind (Section 15.3.4.5; ECMAScript5’s bind also lets you curry arguments, which isn’t done by this implementation). setScope is a particularly unfortunate name, because it doesn’t set the scope, it sets the context.

    Having said all that, there’s no reason you need setScope in your Person constructor. You can just do this:

    Person = function () {
        var self = this;
    
        this.Name = "asd";
    
        var _public = new Object();
        _public.Name = function (value) {
            if (value == undefined) {
                return self.Name;
            } else {
                self.Name = value;
            }
        };
    
        return _public;
    };
    

    Live example

    But using bind (aka setScope) can be useful in places where you don’t want a new closure over the context in which you’re doing it.


    Off-topic: The way you’re specifying Person will break certain things people might expect to work, such as:

    var p = new Person();
    alert(p instanceof Person); // Expect "true", but in your case will be "false"
    

    …because you’re replacing the object new created for you, but returning a different object out of your constructor (which overrides the default).

    Rather than creating a new object and returning that in your constructor, allow the object constructed for you by new to be the object (and thus the Person relationship is maintained), but you can still get truly private variables and use accessors:

    function Person() {
        // Private variable
        var name = "asd";
    
        // Accessor function
        this.Name = function(value) {
            if (typeof value === "undefined") {
                return name;
            }
            name = value;
        };
    }
    

    Live example

    As you can see, this is dramatically simpler, and it preserves the instanceof relationship. Note that we’re not qualifying our references to name within Name at all, and so we’re using the local variable in the constructor call in which our Name function, which closes over it, was created.

    I’ve also taken the liberty there of giving the constructor function a name, because I’m not a fan of anonymous functions. I should have given the accessor a name as well:

    function Person() {
        // Private variable
        var name = "asd";
    
        // Accessor function
        this.Name = Person_Name;
        function Person_Name(value) {
            if (typeof value === "undefined") {
                return name;
            }
            name = value;
        }
    }
    

    Off-topic 2: The overwhelming convention in JavaScript code is to use initial caps on function names only for constructor functions (like Person), and not on other kinds of functions (like Name). You’re free to do whatever you like, of course, but I thought I’d mention the convention, as it makes it easier for other people to read your code.


    Worth noting: All of these techniques result in every single Person object having its own copy of the accessor function. If there are going to be a lot of these objects, that could be a memory issue. If there are only going to be a few, that’s fine.

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

Sidebar

Related Questions

I have an array of function callbacks, like this: class Blah { private var
i have a class of this kind Class Car { private $color; public function
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have a class like this public class foo { private void getThread() {
I have a class which looks something like this: public class Test { private
I have a List<MyClass> The class is like this: private class MyClass { public
Using GWT I have a Java class: public class Pojo { private String name;
I have a class like this: class A { private: B* ptr; } But
I have a struct like this: class Items { private: struct item { unsigned
If I have a simple class setup like this: class MyClass { private string

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.