犀牛书中
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 又是从哪里来的 ???
}
}再次先感谢大家的帮助
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 又是从哪里来的 ???
}
}再次先感谢大家的帮助
举个例子//我定义一个输出人名字的函数
function people(name) {
alert(name);
}
//调用
people('jack'); //jack
//这个时候,客户要求,还要加上年龄,和身高。函数名不能改,参数也不能动。因为客户端已经在使用这个函数了。。你要怎么办?
function people(name) {
return function (age,height) {
alert(name + ',' + age+','+'height');
}
}
//调用
var people = people('jack');
people(26, '176cm');//输出jack,26,176cm