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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:33:23+00:00 2026-05-27T06:33:23+00:00

I have this example code: var foo = { self: this, init: function(){ self.doStuff();

  • 0

I have this example code:

var foo = {

    self: this,

    init: function(){

        self.doStuff();
    },

    doStuff: function(){
        alert('doing stuff');   
    }

}

foo.init();

Why the refence “self” doesn’t work?

Thanks!

  • 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-27T06:33:23+00:00Added an answer on May 27, 2026 at 6:33 am

    Following up on Qeuntin’s response you would use the following to achieve what you’re looking for

    var foo = {
    
        self: false,
    
        init: function(){
            self = this
            self.doStuff();
        },
    
        doStuff: function(){
            alert('doing stuff');   
        },
    }
    

    EDIT: Since it’s been pointed out that whilst this solves OP’s problem (i.e it works) it isn’t exactly how you should go about it. So, here’s a scoping reference.

    function A()
    {
        //Semi-private / hidden var
        var pVar = "I'm a private, err hidden, variable",
            //fn (technically a var)
            pFn = function(){},
            //empty var, placholder for hidden fn
            privatePlaceholderFn;
    
        //Instance-time... public fn
       this.instancePublicFn = function()
        {
            console.log("--- instace public ---");
            //Print hidden var to cosole
            console.log(pVar);
            //Call hidden fn
            instancePrivateFn();
            console.log("--->Setting  private from instance public")
            //Set the hidden fn
            setPrivate();
            console.log("--- / instance public ---");
        }
        //Pass fn to private method.
        this.setPrivFromOutside = function(fn)
        {
            setPrivateFromPrivateYetOutside(fn);
        }
    
        //Set the hidden fn
        this.iPFnPlaceholderSetter = function(fn)
        {
            privatePlaceholderFn = fn;
        }
    
        //Call the semi-private / hidden fn
        this.callPrivate = function()
        {
           privatePlaceholderFn();
        }
        //A misnomer, proves the scope exists. See "function setPrivate()"
        this.setPrivateFromInstance = function()
        {
            //Prove scope exists
            console.log(privatePlaceholderFn);
            console.log("Private From instance - gets inside scope");
    
        }
        //Set hidden fn from private method
        function setPrivate()
        {
            privatePlaceholderFn = function()
            {
                //Show scope exists
                console.log(pVar);
            }
        }
        //Set the hidden fn from hidden method
        function setPrivateFromPrivateYetOutside(fn)
        {
            //fn's scope won't resolve to inside
            privatePlaceholderFn = fn;
        }
        //Private / hidden messager
        function instancePrivateFn()
        {
            //Just loggin' something
            console.log("Instance Private method");
        }
    }
    //Add an object method to the prototype
    A.prototype.protoPuFn = function(){
        console.log("---> Private var from object literal method");
        //console.log(pVar)
    }
    
    //...
    a = new A();
    
    //Add object literal fn
    a.objFn = function()
    {
        console.log("Object literal defined public fn - Gets outside scope");
        //console.log(pVar);
    }
    //Set private / hidden placeholder fn
    a.iPFnPlaceholderSetter(function()
    {
        console.log("Hidden fn, passed through instance public - gets outside scope");
        //console.log(pVar);
    });
    //Attempt to read hidden var
    console.log(a.pVar);
    //Call object literal defined fn
    a.objFn();
    //Call the hidden fn
    a.callPrivate();
    //Call prototype added fn
    a.protoPuFn();
    //Call instance added public fn
    a.instancePublicFn();
    //Call private / hidden method (set
    a.callPrivate();
    //Same as iPFnPlaceholderSetter except the param is passed to a hidden method, before seting.
    a.setPrivFromOutside(function()
    {
        console.log("-->Passed from outside, through public then private setters");
        //console.log(pVar)
    })
    //Call the hidden method
    a.callPrivate();
    //Set hidden fn from instance public
    a.setPrivateFromInstance();
    //Call the hidden method.
    a.callPrivate();
    //Use evi(a)l fn to steal scope.
    a.evil("this.meth = function(){console.log(pVar)}");
    //Call fn with stolen scope
    a.meth();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code for example $(a.foo).bind(function (e){ var t; if ( $(e.target).is(a)
I have such example. function Bar() { this.barVal = BarValue; } function Foo() {
I have this code: class Mailer < ActionMailer::Base def foo recipients bar@example.com from foo@example.com
Has anyone done this / have some example code?
Let's say we have this code (forget about prototypes for a moment): function A(){
i have seen this code : var myNet = require (net); and in some
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()
This code example is from the APE official website: http://www.ape-project.org/ var client = new
I have this simple example I can't seems to get working : MERGE INTO
I have tried this example http://www.bennadel.com/blog/1056-ColdFusion-CFPOP-My-First-Look.htm , but it retrieve emails from POP server.

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.