Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function is not an attribute #216

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

Function is not an attribute #216

Zig1375 opened this issue Nov 15, 2013 · 3 comments

Comments

@Zig1375
Copy link

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;}"/>
@defunctzombie
Copy link
Collaborator

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.

@polotek
Copy link
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.

@defunctzombie
Copy link
Collaborator

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

Zig1375 added a commit to Zig1375/libxmljs that referenced this issue Nov 15, 2013
Attr now doesn't serialize functions (second edition) libxmljs#216
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants