Zoners
Zoners

Reputation: 53

How work the jquery oriented object?

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");
    }
};

Upvotes: 1

Views: 146

Answers (1)

Tei
Tei

Reputation: 1416

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

Upvotes: 4

Related Questions