2016-12-03 115 views
0

這是一段代碼,我無法找到該錯誤,請幫我解決它。Javascript未捕獲的語法錯誤:意外的標識符

<script type="text/javascript"> 
var person={ 
    first_name:"John", 
    last_name:"doe", 
    id:5577 
    fullName function(){ 
     return this.first_name+" "+this.last_name; 
    } 
} 
document.getElementById('demo').innerHTML=person.fullName(); 
</script> 
在這一行

谷歌顯示錯誤

fullName function(){ 
+0

考慮使用諸如[ESLint](http://eslint.org/)或[JSHint](http://jshint.com/)等工具來幫助查找常見的輸入錯誤(以及可選地檢查您選擇的文體規則)。許多編輯至少有一個插件可以讓您在打字時給予反饋。 –

+0

查看JavaScript教程:http://eloquentjavascript.net/04_data.html。 –

回答

1

老同學(Internet Explorer)中,您需要更改爲以前的答案說

var person={ 
    first_name:"John", 
    last_name:"doe", 
    id:5577, 
    // ^missing comma 
    fullName: function(){ 
    // ^missing colon 
     return this.first_name+" "+this.last_name; 
    } 
} 

ES2015(ES6)的簡寫

var person={ 
    first_name:"John", 
    last_name:"doe", 
    id:5577, 
    // ^missing comma 
    fullName(){ 
    // ^no need for "function" keyword 
     return this.first_name+" "+this.last_name; 
    } 
} 
0

你忘了這裏的逗號:

id:5577, 

這裏的冒號:

fullName:function(){ 
0

下面是你的代碼應該看起來像(與你的錯誤在評論RS):

<script type="text/javascript"> 
var person={ 
    first_name:"John", 
    last_name:"doe", 
    id:5577,           //missing coma 
    fullName:function(){        //missing colon 
     return this.first_name+" "+this.last_name; 
    } 
} 

document.getElementById('demo').innerHTML=person.fullName(); 
</script> 
相關問題