疑惑 JS 犀牛书 创建节点方便方法
犀牛书中
Page 338
源代码:
function make(tagname, attributes, children) {
// If we were invoked with two arguments the attributes argument is
// an array or string, it should really be the children arguments.
if (arguments.length == 2 &&
(attributes instanceof Array || typeof attributes == "string")) {
children = attributes;
attributes = null;
}
// Create the element
var e = document.createElement(tagname);
// Set attributes
if (attributes) {
for(var name in attributes) e.setAttribute(name, attributes[name]);
}
// Add children, if any were specified.
if (children != null) {
if (children instanceof Array) { // If it really is an array
for(var i = 0; i < children.length; i++) { // Loop through kids
var child = children;
if (typeof child == "string") // Handle text nodes
child = document.createTextNode(child);
e.appendChild(child); // Assume anything else is a Node
}
}
else if (typeof children == "string") // Handle single text child
e.appendChild(document.createTextNode(children));
else e.appendChild(children); // Handle any other single child
}
// Finally, return the element.
return e;
}
/**
* maker(tagname): return a function that calls make() for the specified tag.
* Example: var table = maker("table"), tr = maker("tr"), td = maker("td");
*/
function maker(tag)
{
return function(attrs, kids)
{
if (arguments.length == 1) return make(tag, attrs);
else return make(tag, attrs, kids);
}
}
中红色标记部分,疑惑描述:
function maker( tag ) // 此处的入口参数就是一个
{
return function( attrs, kids ) // 此处从何得来两个入口参数 ???
{
// 经过测试在该区域块中能取得的数据也只有 tag 如何会有attrs kids
if( argument.length == 1 )
{
return make( tag, attrs ); // attrs 从哪里来的 ???
}
else
return make( tag, attrs, kids ); // kids 又是从哪里来的 ???
}
}
再次先感谢大家的帮助