我想要获得更好的JavaScript工作知识。所以,我购买了Douglas Crockford的书“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);
谢谢Tim,我现在不会再被抓住了:) – CaRDiaK 2009-10-27 11:34:02
@费迪南德:很好的编辑,谢谢 – 2009-10-27 11:43:50