0

I want to know how to create a private attribute in a Javascript class. I tried this:

function Class1(selector)
{
    //calling the constructor
    Constructor();

    //private attribute
    var $container = null;

    function Constructor()
    {
        $container = $(selector);

        //Shows that container is an object
        alert($container);
    }

    function Foo()
    {
         //Shows that container is null
         alert($container);
    }

    result {
        Foo : Foo
    };
}

I supposed that in "Constructor" it creates a new variable $container and assign the object to it. I want to know how I am suposed to assign the value to the attribute $container of the object and not the local variable in the function Constructor.

3
  • 1
    $container = $(selector); will do that, but it seems you are never calling Constructor edit: missed the first line, yeah, the statements are just in the wrong order. That said, I'm not a big fan of emulating visibility this way. IMO it makes the code too complex and inflexible. Rather document your code properly. Commented Mar 20, 2012 at 15:27
  • 1
    Just found it... It's because I call the Constructor Method before I create the variable. So it create one in the function and after it creates the object's one. Commented Mar 20, 2012 at 15:29
  • @Felix Kling I think it makes the code clearer. Just set function that the script is handling on it's own private and the rest public so the user can refer to it. Might not be the best solution but it works and I think it's easier to edit after since all comon function are regrouped together. Commented Mar 20, 2012 at 15:33

2 Answers 2

1

this is because you first call Constructor() and after that you assign null to $container

If you switch that around you will get the desired result:

http://jsfiddle.net/R8RG5/

Sign up to request clarification or add additional context in comments.

1 Comment

Yes thanks I found it... Just couldn't set the question to resolve on my own question but since you answered the solution I found I'll mark your answer as accepted.
0
function Class1(selector) {

    var container = null; //private attribute
    constructor(); //calling the constructor

    function constructor() {
        container = $(selector);
        console.log($container); //Shows that container is an object
    }

    function foo() {
         console.log(container); //Shows that container is null
    }

    result { Foo : foo };
}

such as red-X already told: you have to execute the constructor after initialization of the container variable.

In my example: is is a better practice to use console.log for debugging..

3 Comments

I don't think it's really better practive to use lower case for Class name. I always use Upper Case for class and been told to. If you look at Java, even the class String starts with an Uppercase.
@maniak i wrote "function names" not class names & corrected the class name :)
lol ok! But still think its depend of your style. I red lots of books of video games programming in C++ and a lot of them write functions with uppercase. I don't think it is consider better practice but just two different style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.