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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:58:31+00:00 2026-05-23T02:58:31+00:00

Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()

  • 0

Consider the following code:

function Dog()
{
    this.foo = function() {};

    this.walk = function()
    {
        if(canWalk())
        {
            alert('walking');
            return;
        }

        alert('I have no legs!');
    }

    canWalk = function()
    {
        this.foo();
        return false;
    }
}

var instance = new Dog();
instance.walk();

There is a bug in that code with the “privileged” canWalk() method assuming the this pointer points to the instance of Dog. It actually appears to be pointing to the global object which is confusing me! I’ve read that due to closures I can grab a reference to this in the scope of the constructor then use that reference in my “privileged” method but that seems like a hack.

I just barely grasp the behavior of the this pointer in various contexts. I understand that a method that is “attached” to an object will receive a this pointing to the attached object. For example, foo.doStuff() would have a this pointing to the foo instance.

What’s troubling is that while I think I’m being clever and creating “privileged” methods on my OBJECT, it appears that I’m ACTUALLY dumping functions into global scope! Perhaps there was a global method called init() (quite possible) from another library or file not my own creation. If I then defined a nice little encapsulated object with a “privileged” method called init() I would be replacing the other global init() method.

My question is, what is the best practice for creating “privileged” methods (and fields for that matter) that are scoped to the object they are intended to belong to? What other approach to my sample object would provide private or privileged methods while also being properly scoped. I’m not looking for obscure solutions that meet the requirements but rather what the best practice is.

  • 1 1 Answer
  • 2 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-23T02:58:32+00:00Added an answer on May 23, 2026 at 2:58 am

    Don’t use this. Put var self = this; at the top of your Dog function and use self. That way you will always have a ‘safe’ reference to the Dog.

    In your example this should point to the Dog though unless I’m missing something as well.

    As for your privileged method, you forgot to define the variable. Add var canWalk;

    There is another way: closures. I will give an example:

    function create_dog() {
        function canWalk() {
            return false;
        }
        function walk() {
            if (canWalk()) {
                alert("walking");
                return;
            }
            alert("I have no hands!");
        }
        return {
            "walk": walk // points to the function walk()
        }; // this the API of dog, like public functions (or properties)
    }
    var dog = create_dog();
    dog.walk();
    

    Now you don’t need this or new. Additionally you could do this:

    function create_dog() {
        function canWalk() {
            return false;
        }
        function walk() {
            if (canWalk()) {
                alert("walking");
                return;
            }
            alert("I have no hands!");
        }
        var dog = {
            "walk": walk
        };
        return dog;
    }
    var dog = create_dog();
    dog.walk();
    

    So you can have references to the dog in your priveledges functions. I would advise the closure approach if you’re not going to be using prototyping.

    By the way. To avoid confusion:

    function my_func() {}
    

    and

    var my_func = function () {};
    

    Are equivalent. The latter can be useful if you have a circular reference between two functions and you want to enforce define-before-use.

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

Sidebar

Related Questions

Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider the following snippet of code: function parseXml(xml) { xmlObject= xml; alert(xmlObject.xml); } function
Consider the following JavaScript code which has a recursive boom function function foo() {
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code: class myclass { function __construct(&$arg1, &$arg2) { echo $arg1; echo
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider the following code : HTML: <input type='checkbox' /> <div>Click here</div> JS: $(function() {
Consider the following segment of code: function loadSomeContent() { URLLoader loader = new URLLoader(http://www.somesite.com/);
Consider the following Flex code: private function fullScreenHandler(evt:FullScreenEvent):void { if (evt.fullScreen) { viewstack.selectedIndex =

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.