1

I don't understand how work an object with Jquery/javascript.

And how create a private method/variable with? i see on the forum a closure but i have try and not work. And how see if the method/variable is private? because when I run the website, I see always the function and variable with the own value into my script...

Thanks for your help :).

By e.x:

var ClassName=function()
{
    validation : 0,
    name : 0,
            privateVar: 0,
    init : function ()
    {
        validation = 1;
        name ="toto";
    }
    privatefunction :function()
    {
        alert("a private function");
    }
};
Zoners
  • 53
  • 1
  • 4
  • 12
  • Your example will throw a syntax error. You seem to be mixing up the syntax for object literals and functions. – James Allardice May 04 '12 at 11:36
  • @JamesAllardice I remove the "?" but I test my example and this works.. Do you have a good example to create a jquery object? with private method/variable object? – Zoners May 04 '12 at 11:39
  • 1
    Maybe [this post](http://stackoverflow.com/a/9782779/575527) can help clarify – Joseph May 04 '12 at 11:40
  • @Joseph Thanks your post and the post of Tei it's perfect :) I understand :). – Zoners May 04 '12 at 11:54

1 Answers1

4

Heres one of the multiple ways to have OOP in Javascript

var ClassName = function(){
    var privateVar = 0;

    function privateFunction(){
            alert("a private function");
    }

    return {
        validation : 0,
        name : 0,                
        init : function (){
                validation = 1;
                name ="toto";
        }
    };
};

var myClass = ClassName();
myClass.name = "Foo";
myClass.init(); 

Javascript is not Class based, but prototype based. There are not class**, but instances that can be decorated or used as template to build new instances. This code I have write here have all the proporties of a Class, but is just a instance.

** this is a lie

Tei
  • 1,400
  • 8
  • 6