<body>
<script type="text/javascript">
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[i];
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;
}function maker(tag) {
return function(attrs, kids) {
if (arguments.length == 1) return make(tag, attrs);
else return make(tag, attrs, kids);
}
}var p = make("p", ["This is a ", make("b", "bold"), " word."]);
document.body.appendChild(p);
var table = maker("table"), tr = maker("tr"), td = maker("td");var mytable = table({border:1},tr([th("Name"),th("Type"),th("Value")]));</script>
</body>var table = maker("table"), tr = maker("tr"), td = maker("td");
这句里 返回的是make("table") make("tr") make("td")吗?
var mytable = table({border:1},tr([th("Name"),th("Type"),th("Value")]));这句看不懂,求详细分拆解释
<script type="text/javascript">
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[i];
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;
}function maker(tag) {
return function(attrs, kids) {
if (arguments.length == 1) return make(tag, attrs);
else return make(tag, attrs, kids);
}
}var p = make("p", ["This is a ", make("b", "bold"), " word."]);
document.body.appendChild(p);
var table = maker("table"), tr = maker("tr"), td = maker("td");var mytable = table({border:1},tr([th("Name"),th("Type"),th("Value")]));</script>
</body>var table = maker("table"), tr = maker("tr"), td = maker("td");
这句里 返回的是make("table") make("tr") make("td")吗?
var mytable = table({border:1},tr([th("Name"),th("Type"),th("Value")]));这句看不懂,求详细分拆解释
下面同理
mytable => make("table",{border:1},tr([th("Name"),th("Type"),th("Value")]));
然后这个tr([th("Name"),th("Type"),th("Value")]) = make("tr",[th("Name"),th("Type"),th("Value")],null)能明白了吗
var mytable = table({border:1},tr([th("Name"),th("Type"),th("Value")])) 这话看起来table像个方法了,不明白
mytable => make("table",{border:1},tr([th("Name"),th("Type"),th("Value")]));
var mytable = table({border:1},tr([th("Name"),th("Type"),th("Value")]));
为什么table = make("table", attrs, kids);时有
mytable => make("table",{border:1},tr([th("Name"),th("Type"),th("Value")]));
这个到底是哪方面的表达方式啊??JavaScript权威指南第五版里的ECMAScript也没见到这样表示的,,
2、mytable = table();这个时候table是一个函数,是maker返回的那个函数。
ex: function a() {}; var b = a;这个时候b() == a()同理
javascript权威指南是个不错的参考书,只是核心部分说的都是点到为止,不逐字逐句看,很难看到所以然。
var mytable = table({border:1},tr([th("Name"),th("Type"),th("Value")]));为什么不等效于
var mytable = make({border:1},tr([th("Name"),th("Type"),th("Value")]));而是
mytable = make("table",{border:1},tr([th("Name"),th("Type"),th("Value")]));还有function maker(tag) {
return function(attrs, kids) {
if (arguments.length == 1) return make(tag, attrs);
else return make(tag, attrs, kids);
}
}
if (arguments.length == 1) return make(tag, attrs);这句什么情况下用到?
var table = maker("table"), tr = maker("tr"), th = maker("th");总是else return make(tag, attrs, kids);
也就是说你的table里的第一个参数是个常量了。因为 return function(attrs, kids) {
if (arguments.length == 1) return make(tag, attrs);
else return make(tag, attrs, kids);
}table = function(attrs,kids) {
if(arguments.length == 1) return make(tag,attrs);
else return make(tag,attrs,kids)
}
return function(attrs, kids) {
if (arguments.length == 1) return make(tag, attrs);
else return make(tag, attrs, kids);
}
}
maker这个函数始终是一个参数,然后返回的return function(attrs, kids)2个参数,那样的话
if (arguments.length == 1) return make(tag, attrs);这句好像根本用不上,上面代码中,什么情况会用到 if这句呢
给你举个例子吧
function addGenerator(num) {
//返回一个简单的函数,其中第一个数字来自生成器
return function(toAdd) {
return num + toAdd;
};
}
//addFive现在包含一个接受单一参数的函数,这个函数能求得5加上该参数的和
var addFive = addGenerator(5);
alert(addFive(4) == 9);这是书上的例子~