Function is not an attribute #216

Closed
Zig1375 opened this Issue Nov 15, 2013 · 3 comments

Comments

Projects
None yet
3 participants

Zig1375 commented Nov 15, 2013

Sample code:

Object.prototype.some_function = function() {
    console.log('I am some_function');
};

var data = {'key1' : 'value1', 'key2' : 'value2'};

var libxml = require('libxmljs');
var xml = libxml.Document();
var node = xml.node("query");
node.attr(data);

console.log(node.toString(false));

Result:

<query key1="value1" key2="value2" some_function="function () {&#10;    console.log('I am some_function');&#10;}"/>

@Zig1375 Zig1375 added a commit to Zig1375/libxmljs that referenced this issue Nov 15, 2013

@Zig1375 Zig1375 Attr now doesn't serialize functions #216 a574934
Collaborator

defunctzombie commented Nov 15, 2013

While this seems strange, I do think this is correct behavior. some_function becomes an enumerable property of all objects. Maybe a hasOwnProperty check would be better; that I would consider over a specific function check.

Collaborator

polotek commented Nov 15, 2013

I would rather see this done using Object.keys. The problem is extending the object prototype, not the fact that it's a function. You should try to avoid that in whatever code you're dealing with.

Collaborator

defunctzombie commented Nov 15, 2013

+1 for Object.keys actually. Just skipped my mind.

@Zig1375 Zig1375 added a commit to Zig1375/libxmljs that referenced this issue Nov 15, 2013

@Zig1375 Zig1375 Update element.js
Attr now doesn't serialize functions (second edition) #216
71c25b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment