Opened 13 years ago

Closed 10 years ago

Last modified 10 years ago

#2456 closed defect (worksforme)

[testcase] dojo.clone mangles object methods during clone

Reported by: guest Owned by: James Burke
Priority: high Milestone: 1.4
Component: Core Version: 1.0
Keywords: Cc:
Blocked By: Blocking:

Description (last modified by bill)

Steps to reproduce:

 o = {}
[object Object]
>>> o.f = function () {dojo.debug("SSS");}
function () {...}
>>> o2 = dojo.lang.shallowCopy(o, true)
[object Object]
>>> o.f()
SSSdojo.js (line 237)
>>> o2.f()
>>> o2 = dojo.lang.shallowCopy(o, false)
[object Object]
>>> o2.f()
SSS

Dojo 0.4.1 on Firefox 2.0/Ubuntu Linux 6.10

My contact info: apa3a at yahoo dot com

Attachments (3)

dojo._base.lang.clone (422 bytes) - added by matsuri 11 years ago.
dojo.tests._base.lang (332 bytes) - added by matsuri 11 years ago.
util.doh.runner (838 bytes) - added by matsuri 11 years ago.

Download all attachments as: .zip

Change History (17)

comment:1 Changed 12 years ago by bill

Milestone: 1.1

comment:2 Changed 12 years ago by Adam Peller

Still a problem in 1.x

>>>f2=dojo.clone(function(){console.log("foo");})
foo
anonymous()
>>>f2
anonymous()
>>>f2() // f2 is a function created from Function.constructor; it doesn't do anything

comment:3 Changed 12 years ago by dylan

Owner: changed from anonymous to alex
Priority: highnormal
severity: majornormal
Summary: dojo.lang.shallowCopy mangles object methods during deep copy[testcase] dojo.clone mangles object methods during clone
Version: 0.4.11.0

The testcase provided by peller is still an issue.

comment:4 Changed 11 years ago by bill

Milestone: 1.11.2

Move all milestone 1.1 tickets to 1.2, except for reopened tickets and tickets opened after 1.1RC1 was released.

Changed 11 years ago by matsuri

Attachment: dojo._base.lang.clone added

Changed 11 years ago by matsuri

Attachment: dojo.tests._base.lang added

Changed 11 years ago by matsuri

Attachment: util.doh.runner added

comment:5 Changed 11 years ago by matsuri

what ive done

i have 3 patches maybe solving the problem

  1. dojo._base.lang.clone: i added a part to clone functions (was missing before)
  1. dojo.tests._base.lang: i outcommented the error throwing part (with the function)
  1. util.doh.runner: i implemented a funcEq function (which is very simple and surely not enough.. but i dont know better now) and adjusted the assertEqual method

what should probably be done next

  • testing the stuff by an experienced person (knowing all the js func secrets i dont know)
  • maybe adding the funcEq to the other asserts

comment:6 Changed 11 years ago by bill

Milestone: 1.21.3

as per today's meeting, punting these core bugs

comment:7 Changed 11 years ago by bill

Description: modified (diff)
Milestone: 1.3future

comment:8 Changed 11 years ago by bill

Owner: changed from alex to James Burke

comment:9 Changed 11 years ago by Eugene Lazutkin

Again, if you look at the description, it is the ticket for 0.4.1. Are we fixing those? Proposal: close as wontfix.

comment:10 in reply to:  9 Changed 11 years ago by Adam Peller

Replying to elazutkin:

Again, if you look at the description, it is the ticket for 0.4.1. Are we fixing those? Proposal: close as wontfix.

See my comment above. It's been confirmed on Dojo 1.x.

comment:11 Changed 11 years ago by Eugene Lazutkin

OK, I missed the test in 1.0, sorry. But just FYI: there is no way to clone a function in JavaScript. There is a hack that works in some browsers, but it is not supported in the language.

The hack:

var f = eval("(" + old.toString() + ")");

In general dojo.clone() was meant to be used only for value-based objects. I don't recall now why it clones DOM nodes too, but it is a problem that bites users time to time.

comment:12 Changed 10 years ago by petschm

I have the same problem cloning something simple like

var test = {
	fu: function () {
		//do something
	},
	bar:"something"
};

The function fu doesn't work anymore in the cloned object.

Why not just return the function in dojo.clone()?

if(dojo.isFunction(o))
    return o;

comment:13 Changed 10 years ago by Eugene Lazutkin

Resolution: worksforme
Status: newclosed

I believe it was fixed in the course of other tickets. Please reopen if you still have problems.

comment:14 Changed 10 years ago by Adam Peller

Milestone: future1.4

WFM, and thanks for adding a test case.

Note: See TracTickets for help on using tickets.