CaR*_*iaK 1 javascript prototype
我正在努力获得更好的JavaScript工作知识.所以,我已经买了道格拉斯·克罗克福德的书"JavaScript的好部分".
我现在很难掌握Prototype.下面的所有内容似乎都在我的浏览器中工作,直到我点击// PROTOTYPE示例.有人可以看看它,看看为什么我不能从中得到任何输出.(除非我将所有原型代码注释掉,否则我的页面将返回空白)
谢谢你的帮助.
巴里
var stooge = {
"first-name": "Jerome",
"last-name": "Howard",
"nickname": "J",
"profession" : 'Actor'
};
// below is augmenting
var st = stooge;
st.nickname = "curly";
// st.nickname and nick are the same because both are ref's to the same object
var nick = st.nickname;
document.writeln(stooge['first-name']); //expect Jerome -- this is "suffix" retrieval
document.writeln(st.nickname); //expect "curly" -- this is "notation" retrieval
document.writeln(nick); //expect "curly"
document.writeln(stooge.profession);
//PROTOTYPE EXAMPLE;
if (typeof Object.create !== 'function')
{
object.create = function(o) {
var F = function () {};
F.prototype = o;
return new F();
};
var another_stooge = Object.create(stooge);
another_stooge['first-name'] = 'Barry';
document.writeln(another_stooge['first-name']);
// the below should be inherited from the prototype therefore "Actor"
document.writeln(another_stooge.profession);
Run Code Online (Sandbox Code Playgroud)
你在分配给object.create的函数表达式的末尾缺少一个右括号,而且你还没有大写的Object object.create = function(o) {.
//PROTOTYPE EXAMPLE;
if (typeof Object.create !== 'function')
{
Object.create = function(o) { // <--- "Object" instead of "object"
var F = function () {};
F.prototype = o;
return new F();
};
} // <--- Closing brace was missing
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
752 次 |
| 最近记录: |