[Proto-Scripty] Re: Class.create() and Object.clone() and methods

2010-12-23 Thread thomas.boer...@googlemail.com
HI!

Great! Thanks!

Thomas

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: Class.create() and Object.clone() and methods

2010-12-17 Thread T.J. Crowder
Hi,

 Are the methods on the class level, as if I had declared them with
 Database.prototype.methodName?

Well, at the prototype level, yes. (class level gets to be
ambiguous, since class methods in many languages are meant to be
ones that are on the *class*, not instances of it; e.g., like
Object.clone.) The functions are shared across all instances created
via `Database`, they're not duplicated as with the Crockford private
methods pattern. This is easily demonstrated:

* * * *
  var d1 = new Database();
  var d2 = new Database();
  display(Database.prototype.setName === d1.setName?  +
  (Database.prototype.setName === d1.setName));
  display(d1.setName === d2.setName?  +
  (d1.setName === d2.setName));
* * * *
http://jsbin.com/ataze

 If this is true, is it also true, if I clone such an object with
 Object.clone()?

If object `a` has properties referencing functions, and you clone it
to `b`, then `b` uses the *same* function instances that `a` uses; the
functions aren't duplicated:
* * * *
  var a = {
func: function() {
  display(My name is  + this.name);
},
name: 'a'
  };

  var b = Object.clone(a);
  b.name = 'b';

  a.func();
  b.func();
  display(a.func === b.func?  + (a.func === b.func));
* * * *
http://jsbin.com/abeqe3

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Dec 15, 3:41 pm, thomas.boer...@googlemail.com
thomas.boer...@googlemail.com wrote:
 HI!

 When I create a class with Class.create() and add some methods like
 this:

 var Database = Class.create({
 initialize: function() {
   this.name = ;},

 setName: function(name) {
   this.name = name;

 },
 });

 Are the methods on the class level, as if I had declared them with
 Database.prototype.methodName?

 So they do not cost memory, if I use new to instantiate objects from
 this class, correct?

 If this is true, is it also true, if I clone such an object with
 Object.clone()?

 Thanks!

 Thomas

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.