[Proto-Scripty] Re: strange problem with detect method and cloning data

2011-10-18 Thread T.J. Crowder
On Oct 18, 6:01 am, buda www...@pochta.ru wrote:
 here the code http://jsfiddle.net/QW8vM/10/

`Object.clone` returns a raw object with a shallow copy of the
properties of the object you give it. It is not a perfect copy of the
object down to the prototype level:

var a, b;
a = [1, 2, 3];
b = Object.clone(a);
display(Object.isArray(a):  + Object.isArray(a)); // true
display(Object.isArray(b):  + Object.isArray(b)); // false ===

If you want to copy an array, use Array#slice (but note that it only
copies numeric properties, so if you've added non-numeric properties
to the array, you'll have to handle them):

var a, b;
a = [1, 2, 3];
b = a.slice(0); // Copy the array
display(Object.isArray(a):  + Object.isArray(a)); // true
display(a =  + a.join(, )); // 1, 2, 3
display(Object.isArray(b):  + Object.isArray(b)); // true
display(b =  + b.join(, )); // 1, 2, 3

Here's a working version of your fiddle:
http://jsfiddle.net/QW8vM/11/

Somewhat off-topic, but I find that `a` constructor very odd. `add`
always adds to a central array shared by all instances created by the
`a` constructor, but *every access* to the `items` property *copies*
that central array. Making a copy of an array on property access is
very surprising behavior, that's normally the sort of thing a function
is for. Consider:

var foo = new a();
var x = foo.items;
var y = foo.items;

Anyone reading that could would expect that `x` and `y` referred to
the same array, but of course they don't in the case of your `a`
constructor. Very surprising. Similarly:

var one = new a();
var two = new a();
one.add(foo);
one.add(bar);
alert(two.items.length); // 2?!?! I haven't added anything to `two`!

I'm curious what the use-case is...

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

-- 
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-scriptaculous@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: strange problem with detect method and cloning data

2011-10-18 Thread buda
it's general example for show the problem with Object.clone :)

Usually I use anothe technique:

var a = Class.create((function(){.

  var instancePrivates = [];

  function initialize(){
  var internalId;
  
  instancePrivates.push({});
  internalId = --instancePrivates;
  instancePrivates[internalId].items = []  --- here instance private 
items !!! :)





-- 
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/-/4fipXTWMndwJ.
To post to this group, send email to prototype-scriptaculous@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: strange problem with detect method and cloning data

2011-10-18 Thread buda
in last example of course 

this.internalId  = --instancePrivates;

instead of 

var internalId;
internalId = --instancePrivates;


-- 
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/-/Kn_ZZp4sFXcJ.
To post to this group, send email to prototype-scriptaculous@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: strange problem with detect method and cloning data

2011-10-18 Thread buda
that sample was to demonstarte Object.clone bug :)
Usually I us to

var a  = Class.create((function() {

   var _privates = [];
   
   function initialize() {
  _privates.push({});
  this.internalId = _privates.length-1;
  _privates[this.internalId].items = [];  -- here the instances items
  ...

-- 
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/-/vl61KSkPtYIJ.
To post to this group, send email to prototype-scriptaculous@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: strange problem with detect method and cloning data

2011-10-18 Thread T.J. Crowder
On Oct 18, 10:29 am, buda www...@pochta.ru wrote:
 that sample was to demonstarte Object.clone bug :)

It's not a bug, though I'd say detecting that it's being fed an array
wouldn't be a bad feature to add.

 Usually I us to

 var a  = Class.create((function() {

    var _privates = [];

    function initialize() {
       _privates.push({});
       this.internalId = _privates.length-1;
       _privates[this.internalId].items = [];  -- here the instances items
       ...

How do you ever clean up the private data for instances that have been
released and reclaimed by the GC? Some kind of destroy contract and
hope you don't miss out a destroy call?

-- T.J.

-- 
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-scriptaculous@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: Why created object is not extended with Object methods?

2011-10-18 Thread T.J. Crowder
On Oct 18, 8:30 am, buda www...@pochta.ru wrote:
 I try

 var o = {};
 o.name = 'propertyName';
 alert(o.keys); -- undefined

 why? but in debugger I see that Object has all methods, but o - not!

`keys` is not an instance method, it's a method of the `Object`
function:

alert(Object.keys(o)); // name

You're confusing `Object` with `Object.prototype`. The properties of
`Object` are not inherited by object instances; the properties of
`Object.prototype` are.
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

-- 
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-scriptaculous@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: Why created object is not extended with Object methods?

2011-10-18 Thread buda
T.J. what the reasons not to make them Object.prtototype methods?

On 18 окт, 14:21, T.J. Crowder t...@crowdersoftware.com wrote:
 On Oct 18, 8:30 am, buda www...@pochta.ru wrote:

  I try

  var o = {};
  o.name = 'propertyName';
  alert(o.keys); -- undefined

  why? but in debugger I see that Object has all methods, but o - not!

 `keys` is not an instance method, it's a method of the `Object`
 function:

 alert(Object.keys(o)); // name

 You're confusing `Object` with `Object.prototype`. The properties of
 `Object` are not inherited by object instances; the properties of
 `Object.prototype` are.
 --
 T.J. Crowder
 Independent Software Engineer
 tj / crowder software / com
 www / crowder software / com

-- 
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-scriptaculous@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: strange problem with detect method and cloning data

2011-10-18 Thread buda
I use destroy method on the class and clean everythig in it

On 18 окт, 14:17, T.J. Crowder t...@crowdersoftware.com wrote:
 On Oct 18, 10:29 am, buda www...@pochta.ru wrote:

  that sample was to demonstarte Object.clone bug :)

 It's not a bug, though I'd say detecting that it's being fed an array
 wouldn't be a bad feature to add.

  Usually I us to

  var a  = Class.create((function() {

     var _privates = [];

     function initialize() {
        _privates.push({});
        this.internalId = _privates.length-1;
        _privates[this.internalId].items = [];  -- here the instances items
        ...

 How do you ever clean up the private data for instances that have been
 released and reclaimed by the GC? Some kind of destroy contract and
 hope you don't miss out a destroy call?

 -- T.J.

-- 
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-scriptaculous@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: Why created object is not extended with Object methods?

2011-10-18 Thread T.J. Crowder
On Oct 18, 3:08 pm, buda www...@pochta.ru wrote:
 T.J. what the reasons not to make them Object.prtototype methods?

If you add something to `Object.prototype`, it shows up on *every*
object. So for instance:

Object.prototype.foo = function() {
return bar;
};
var a = {};
console.log(typeof a.foo);
// - function
for (var name in a) {
console.log(name);
}
// - foo

To put it mildly, this causes a problem because people expect a blank
object to be, um, blank. And in fact, you see people repeatedly
running into problems with Prototype's additions to `Array.prototype`
because they're using `for..in` incorrectly[1][2]. Compare the output
of this page:
http://jsbin.com/ajiyal
with this one:
http://jsbin.com/ajiyal/2

The code is identical in the two pages, it's just that the second one
includes Prototype and the first one doesn't. Since Prototype adds a
bunch of `Array.prototype` properties, they show up on that naive
(broken) `for..in` loop.

As of ECMAScript5 it's possible to put properties on
`Object.prototype` (and `Array.prototype`) that *won't* break naive
`for..in` loops by using `Object.defineProperty` and setting
`enumerable` to `false`, but there's still the problem that people
expect a blank object to be blank. And so if (for instance) you put a
`keys` property on `Object.prototype` that refers to a function that
returns the object's keys, but I have code where I use a `keys`
property on an object to mean something else entirely (very likely,
and in fact I *have* done it), there's a problem. For that reason, the
list of properties defined for `Object.prototype` in the spec[3] is
very short and likely to stay that way, in favor of properties defined
on `Object` that you pass an instance into (like `Object.keys`).

[1] http://blog.niftysnippets.org/2010/11/myths-and-realities-of-forin.html
[2] http://api.prototypejs.org/language/Array/
[3] http://es5.github.com/#x15.2.4

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

-- 
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-scriptaculous@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.



RE: [Proto-Scripty] Re: Why created object is not extended with Object methods?

2011-10-18 Thread wwwboy
thanks for the explanation!

-Original Message-
From: prototype-scriptaculous@googlegroups.com
[mailto:prototype-scriptaculous@googlegroups.com] On Behalf Of T.J. Crowder
Sent: Tuesday, October 18, 2011 6:52 PM
To: Prototype  script.aculo.us
Subject: [Proto-Scripty] Re: Why created object is not extended with Object
methods?

On Oct 18, 3:08 pm, buda www...@pochta.ru wrote:
 T.J. what the reasons not to make them Object.prtototype methods?

If you add something to `Object.prototype`, it shows up on *every* object.
So for instance:

Object.prototype.foo = function() {
return bar;
};
var a = {};
console.log(typeof a.foo);
// - function
for (var name in a) {
console.log(name);
}
// - foo

To put it mildly, this causes a problem because people expect a blank object
to be, um, blank. And in fact, you see people repeatedly running into
problems with Prototype's additions to `Array.prototype` because they're
using `for..in` incorrectly[1][2]. Compare the output of this page:
http://jsbin.com/ajiyal
with this one:
http://jsbin.com/ajiyal/2

The code is identical in the two pages, it's just that the second one
includes Prototype and the first one doesn't. Since Prototype adds a bunch
of `Array.prototype` properties, they show up on that naive
(broken) `for..in` loop.

As of ECMAScript5 it's possible to put properties on `Object.prototype` (and
`Array.prototype`) that *won't* break naive `for..in` loops by using
`Object.defineProperty` and setting `enumerable` to `false`, but there's
still the problem that people expect a blank object to be blank. And so if
(for instance) you put a `keys` property on `Object.prototype` that refers
to a function that returns the object's keys, but I have code where I use a
`keys` property on an object to mean something else entirely (very likely,
and in fact I *have* done it), there's a problem. For that reason, the list
of properties defined for `Object.prototype` in the spec[3] is very short
and likely to stay that way, in favor of properties defined on `Object` that
you pass an instance into (like `Object.keys`).

[1] http://blog.niftysnippets.org/2010/11/myths-and-realities-of-forin.html
[2] http://api.prototypejs.org/language/Array/
[3] http://es5.github.com/#x15.2.4

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

--
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-scriptaculous@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.


-- 
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-scriptaculous@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.