Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #24192
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2014-05-16 12:25 -0700 |
| Message-ID | <babeaadf-e17c-4c4b-9894-5027d193e7c5@googlegroups.com> (permalink) |
| Subject | Now that's REALLY stupid. |
| From | dhtml <dhtmlkitchen@gmail.com> |
http://yuilibrary.com/yui/docs/api/files/node_js_node-core.js.html#l8
They try to return null in the middle of the constructor. Constructor function (new expression) can never result in a primitive value.
function ASDF() { return null; }
var a = new ASDF();
a // object.
Describing how `new` works is great for newbies and beginners to learn. And also those publishing a state-of-the-art javascript library since around 2006 (nearly eight years), who wish the constructor to result null.
var foofer = Y.Node("foofer");
foofer === null; // nope!
Y_Node what is assigned to Y.Node, and they try to make it so.
Y_Node = function(node) {
if (!this.getDOMNode) { // support optional "new"
return new Y_Node(node);
}
if (typeof node == 'string') {
node = Y_Node._fromString(node);
if (!node) {
return null; // NOTE: return
}
}
Function Y_Node calls itself again in newExpression, if it has been called without new:
if (!this.getDOMNode) { // support optional "new"
And then they try to return null:
if (!node) {
return null; // NOTE: return
}
The problem is that javascript doesn't work like that.
So I'm replacing all DOM element reference to Y.Node decorated elements, using a call to `new Y.Node()` (code requirements here). So where I had `getElementById`, which will return null when the element with the id is not found, Y.Node tries to mimic that by returning null. But again, it doesn't work that way.
Here's their code:
/**
* The Node Utility provides a DOM-like interface for interacting with DOM nodes.
* @module node
* @main node
* @submodule node-core
*/
/**
* The Node class provides a wrapper for manipulating DOM Nodes.
* Node properties can be accessed via the set/get methods.
* Use `Y.one()` to retrieve Node instances.
*
* <strong>NOTE:</strong> Node properties are accessed using
* the <code>set</code> and <code>get</code> methods.
*
* @class Node
* @constructor
* @param {HTMLElement} node the DOM node to be mapped to the Node instance.
* @uses EventTarget
*/
// "globals"
var DOT = '.',
NODE_NAME = 'nodeName',
NODE_TYPE = 'nodeType',
OWNER_DOCUMENT = 'ownerDocument',
TAG_NAME = 'tagName',
UID = '_yuid',
EMPTY_OBJ = {},
_slice = Array.prototype.slice,
Y_DOM = Y.DOM,
Y_Node = function(node) {
if (!this.getDOMNode) { // support optional "new"
return new Y_Node(node);
}
if (typeof node == 'string') {
node = Y_Node._fromString(node);
if (!node) {
return null; // NOTE: return
}
}
var uid = (node.nodeType !== 9) ? node.uniqueID : node[UID];
if (uid && Y_Node._instances[uid] && Y_Node._instances[uid]._node !== node) {
node[UID] = null; // unset existing uid to prevent collision (via clone or hack)
}
uid = uid || Y.stamp(node);
if (!uid) { // stamp failed; likely IE non-HTMLElement
uid = Y.guid();
}
this[UID] = uid;
/**
* The underlying DOM node bound to the Y.Node instance
* @property _node
* @type HTMLElement
* @private
*/
this._node = node;
this._stateProxy = node; // when augmented with Attribute
if (this._initPlugins) { // when augmented with Plugin.Host
this._initPlugins();
}
},
Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread
Now that's REALLY stupid. dhtml <dhtmlkitchen@gmail.com> - 2014-05-16 12:25 -0700
csiph-web