Re: [Proto-Scripty] как создавать в классе правильно частные и публичные методы и свойства

10 views
Skip to first unread message

Wojtek Zadora

unread,
Jan 7, 2013, 4:03:21 AM1/7/13
to prototype-s...@googlegroups.com
I read and understand a lot of russian but I'd rather not try to write it.
Hope answer in english will be of any help to you.

To have private functions and properties you need to use clousure like this:

var AClass = Class.create((function()
{
var _stat = 'I am static private';

function initialize()
{
this.test = 'I am private';
}

function _priv()
{
console.log(this.test);
}

function _private()
{
console.log(_stat);
}

return {
initialize: initialize,

pub: function()
{
_priv.apply(this);
},

publi: function()
{
_private();
},

setpubli: function()
{
_stat = 'after set';
}
};
})());

document.observe("dom:loaded", function()
{
var a = new AClass();
a.pub();
a.publi();
a.setpubli();

var b = new AClass();
b.publi();
});

Inheritance:

var ASubClass = Class.create(AClass, (function()
{
function initialize($super)
{
$super($A(arguments));
}

return {
initialize: initialize,

pfun: function()
{
console.log('in sub ');
}
};
})());


There is much more to explore on this subject.

Have fun





> О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫
> var myClass=Class.create (
> initialize :function(){} ,
> action:function () {}
> )
> О©╫О©╫О©╫О©╫О©╫ action О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫(О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫)...О©╫ О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ О©╫О©╫О©╫О©╫О©╫
> --
> You received this message because you are subscribed to the Google
> Groups "Prototype & script.aculo.us" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/prototype-scriptaculous/-/s5jE0forsHgJ.
> To post to this group, send email to
> prototype-s...@googlegroups.com.
> To unsubscribe from this group, send email to
> prototype-scripta...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/prototype-scriptaculous?hl=en.

Wojtek Zadora

unread,
Jan 7, 2013, 4:23:47 AM1/7/13
to prototype-s...@googlegroups.com
Sorry, one not so small mistake: should be

function initialize()
{
this.test = 'I am public';
}

this is public of course

Александр Павленко

unread,
Jan 7, 2013, 11:05:14 AM1/7/13
to prototype-s...@googlegroups.com
а вот так 
  var myClass= Class.create(
    {  initialize:function(str)
        {   this.str=str ;
            this.msg=function()
                {alert ("privat"+this.str);}
        } ,
       public:function()
            {alert ("public"+this.str)}
    }
  )

метод msg  приватный или нет

fntzr

unread,
Jan 7, 2013, 1:59:28 PM1/7/13
to prototype-s...@googlegroups.com
No, this method `msg` is public.

But if you create

var myClass= Class.create(
{ initialize:function(str)
{ this.str=str ;
msg=function()
{alert ("privat"+this.str);}
} ,
public:function()
{alert ("public"+this.str)}
}
)

then `msg` is private. All method that your add to `this` will be public.
For creating private methods use that was advised or agreements (for
example: all private methods started with __ -> __private_method,
public_method).

Александр Павленко

unread,
Jan 7, 2013, 5:18:24 PM1/7/13
to prototype-s...@googlegroups.com


понедельник, 7 января 2013 г., 20:59:28 UTC+2 пользователь mikhail написал:
но  в таком случае  msg  получается глобальной...это не совсем хорошо..правда??
Reply all
Reply to author
Forward
0 new messages