Skip to content

Explaining (multiple) arguments to .Public .Protected .Private

rodyhaddad edited this page Sep 17, 2012 · 1 revision

You can pass multiple arguments to .Public/.Protected/.Private, allowing you to define all you properties and methods in one call.

Let's start by looking at the basic usage of .Public/.Protected/.Private.

###Properties

To define a property, you can do:

.Public("propertyName")

If you want to add a default value to that property, just do:

.Public("propertyName", yourDefaultValue)//Now all instance of the Class will start with "propertyName" === yourDefaultValue.  

###Methods

To define a method, you can do:

.Public(function yourMethodName(){ //you would be using a named function expression

})

If you don't want to use a named function expression, you can do:

.Public("yourMethodName", function(){

})

###Multiple arguments to .Public/.Protected/.Private

You can define many properties and methods in just one call, like this:

.Public(
    "property1",
    "property2",
    function method1(){
        
    },
    function method2(){
        
    },
    //the order doesn't matter
    "property3",
    function method3(){
        
    }
)

If you want to set a property's defaultValue, or don't want to use named function expressions, then use objects like this:

.Public(
    "property1",
    {property2: defaultValue},
    {
        method1: function(){ // no named function expression

        }
    },
    //you can also combine many properties/methods in one object
    {
        property3: aDefaultValue,
        method3: function(){
            
        },
        method4: function(){
        
        }
    }
)

If you want to define multiple properties/methods with one .Public call, and you don't want to use named function expression, then I suggest you write it like this:

.Public(
    "property1",
    "property2",
    "property3",
{
    "property4": gotADefaultValue,
    method1: function(){
        
    },
    method1: function(){
        
    },
    method1: function(){
        
    }
})

But if you don't care about using named function expression, then I suggest you write it like this:

.Public(
    "property1",
    "property2",
    "property3",
    {"property4": gotADefaultValue},
    function method1(){
        
    },
    function method1(){
        
    },
    function method1(){
        
    }
)

These are my style-suggestion for defining your properties/methods in just one call. But feel free to declare your properties/methods as you'd like :)

Just keep in mind that if you just pass 2 arguments to .Public/.Protected/.Private, and the first argument is a string, then the call would be handle as a 1 property(or method) declaration:

.Public(
    "property1",
    "property2" // !: this will be interpreted as the property "property1" being declared, with a defaultValue of "property2"
)
//this is the same as:
.Public("property1", "anyRandomDefaultValue")

Also: by declaring many properties/methods in just one call, you won't be able to apply plugins to your properties/methods. (TODO: link to plugin page)