JavaScript-原型
我正在努力获得更好的JavaScript工作知识。所以,我买了Douglas Crockford的《JavaScript的好部分》一书 目前我很难掌握原型。在我点击//原型示例之前,以下所有内容似乎都在我的浏览器中工作。有人可以看一看,看看为什么我不能从中得到任何输出。(除非我注释掉所有原型代码,否则我的页面返回空白) 谢谢你的帮助 巴里JavaScript-原型,javascript,prototype,Javascript,Prototype,我正在努力获得更好的JavaScript工作知识。所以,我买了Douglas Crockford的《JavaScript的好部分》一书 目前我很难掌握原型。在我点击//原型示例之前,以下所有内容似乎都在我的浏览器中工作。有人可以看一看,看看为什么我不能从中得到任何输出。(除非我注释掉所有原型代码,否则我的页面返回空白) 谢谢你的帮助 巴里 var stooge = { "first-name": "Jerome", "last-name": "Howard", "nic
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);
您似乎缺少行
object.create=function(o){
…我看到if语句和var F=function(){};
的右大括号,但没有看到function(o)
的右大括号
缺少右大括号确实会抑制输出,因为Javascript会假定(缺少的)右大括号之前的所有内容都是函数定义的一部分,而不是要执行的内容。您似乎缺少行
对象的右大括号。create=function(o){
…我看到if语句和var F=function(){};
有一个右大括号,但是函数(o)
没有
缺少右大括号确实会抑制输出,因为Javascript会假定(缺少的)右大括号之前的所有内容都是函数定义的一部分,而不是要执行的内容
//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
//原型示例;
if(type of Object.create!=“函数”)
{
Object.create=function(o){/在分配给Object.create的函数表达式末尾缺少一个右括号,而且在Object.create=function(o){
中没有将Object大写
//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
//原型示例;
if(type of Object.create!=“函数”)
{
Object.create=函数(o){//谢谢你。两个答案都准确地解决了我的问题。我将把Tim标记为答案,因为他分数较少,并且指出了O的大小写,但非常感谢mark你帮助我理解:)谢谢你。两个答案都准确地解决了我的问题。我将把Tim标记为答案,因为他分数较少,并且指出了答案O的大写字母,但非常感谢马克你帮助我理解了:)谢谢你,提姆,我现在再也不会被这件事弄糊涂了:)谢谢你,提姆,我现在再也不会被那件事弄糊涂了:)