By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

EXTEND ?

P: n/a
I switched to using this function to create element:
----------------------------------------------------
function elem(name, attrs, style, text) {
var e = document.createElement(name);
if (attrs) {
for (key in attrs) {
if (key == 'class') {
e.className = attrs[key];
} else if (key == 'id') {
e.id = attrs[key];
} else {
e.setAttribute(key, attrs[key]);
}
}
}
if (style) {
for (key in style) {
e.style[key] = style[key];
}
}
if (text) {
e.appendChild(document.createTextNode(text));
}
return e;
}
---------------------------------------------------------
so instead of the "old" way:
var oMyDiv = document.createElement("DIV");
oMyDiv.setAttribute("id","testid");
oMyDiv.className = "testclass";

I use that function :
var oMyDiv = elem('DIV', {'id':'testid', 'class':'testclass'});

The rendered source shows this:
<div extend=" function (object) {return Object.extend.apply(this, [this,
object]); } "
class="testclass" id="testid">
Question: I don';t understand the "EXTEND" part......anyways does it look
legit/right ?
Any if you please put this "extend=" function (object) {return
Object.extend.apply(this, [this, object]); } "
in English ;)

thanks
Sep 24 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Boobie wrote:
Any if you please put this "extend=" function (object) {return
Object.extend.apply(this, [this, object]); } "
in English ;)


Here's some info on the Object.apply method:

http://developer.mozilla.org/en/docs...Function:apply
JW

Sep 24 '05 #2

P: n/a
"Boobie" <bo********@mailnull.com> writes:
function elem(name, attrs, style, text) { for (key in attrs) { .... e.setAttribute(key, attrs[key]); .... var oMyDiv = elem('DIV', {'id':'testid', 'class':'testclass'});
.... giving:
<div extend=" function (object) {return Object.extend.apply(this, [this,
object]); } "
class="testclass" id="testid"> Question: I don';t understand the "EXTEND" part......anyways does it look
legit/right ?


It seems you have givent the Object.prototype an extra property, i.e.,

Object.prototype.extend = function(object) {
return Object.extend.apply(this, [this, object]);
};

Since that means that *all* objects inherit the "extend" property,
also your "attrs" object will have it, so it has three attributes:
"id", "class" *and* "extend". The last one is a function, which
gives the value you see when it is converted to a string.

This is a good example of why extending Object.prototype is dangerous,
and should only be done with extreme care, since all other code running
in the same environment should know about it.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 24 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.