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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:29:34+00:00 2026-06-11T14:29:34+00:00

I have a javascript class that I’m creating. This class has private and public

  • 0

I have a javascript class that I’m creating. This class has private and public functions/properties. My understanding of private and public was that this was public and var was private to that function and its members. However, within the local function buildFramework(), when I call the var settings.currentView I get the error:

settings.currentView is not defined

My question is, what is the difference between this and var in the scope of a function and its members as well as the global scope?

namespace('example');
example.InstagramViewer = function (options) {
    // this works when called within buildFramework()
    this.settings = $.extend({
        currentView: 'grid'
    }, options);

    // this doesn't work when called within buildFramework()
    var settings = $.extend({
        currentView: 'grid'
    }, options);

    var viewer;

    this.init = function () {
        buildFramework();
    };

    var buildFramework = function() {
        viewer = $(viewerWrapper).append('<div id="instagramViewer" class="' + settings.currentView + '"></div>'); // this doesn't work
        viewer = $(viewerWrapper).append('<div id="instagramViewer" class="' + this.settings.currentView + '"></div>'); // this does work
    };
}

and called like so…

$(function () {
    var viewer = new connectionsAcademy.publicWebsite.web.js.teenWebsite.InstagramViewer();
    viewer.init();
});
  • 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-11T14:29:35+00:00Added an answer on June 11, 2026 at 2:29 pm

    There are a lot of differences here.

    First, ‘this’ is a keyword which is a reference to the calling object. If you call your function as is, the window will be the calling object and you are setting a global variable by using the the keyword ‘this’.

    InstagramViewer();
    console.log(window.settings);
    

    However, if you call your function through another object, window will no longer be the calling object and instead of setting a global variable you will be setting a member variable on that object.

    var obj = {InstagramViewer: InstagramViewer};
    obj.InstagramViewer();
    console.log(obj.settings);
    

    using ‘var’ in both these cases makes no difference. ‘var’ works different then ‘this’ because it affects no object besides what we will call the function’s scratchpad. Only the function can access its scratchpad (unless you create some kind of closure which exposes a variable on this scratch pad). This is why you can think of a variable defined with ‘var’ as being private.

    using ‘var’ is also different then ‘this’ when dealing with ‘delete’. ‘delete’ will not work on a variable declared with ‘var’, but it will when defining a variable on ‘this’ or any other object.

    var F = function() {
        this.foo = 'foo';
        var bar = 'bar';
    
        delete(bar);
        delete(this.foo);
    
        alert(bar);
        alert(this.foo);
    };
    
    
    F();
    

    My explanation is crude, but this is a big subject that can not be easily explained in this context. I highly recommend you read this book http://shop.oreilly.com/product/9780596000486.do. Especially chapter 7.

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

Sidebar

Related Questions

I have a JavaScript class that has a method: function someClass() { this.someMethod =
Let's say you have a JavaScript class like this var DepartmentFactory = function(data) {
I have a javascript class that looks like this: function xyPoint(x, y) { this.
I have a JavaScript class that looks like this: function SomeFunction() { this.doSomething(function() {
I have a Javascript class that contains a few functions and member objects: function
The following is a Javascript class that I have created. (function(window){ function Project(name,description){ this.name
I have a function in javascript that changes a span class when I click
I have the following class in javascript: function User(aJid){ this.jid = aJid; this.name =
I have in my javascript these 2 functions classes: // product class function Product()
I have a JavaScript class defined as shown here: function Item() { this.init(); }

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.