[英]Define object method in javascript
我想在javascript中創建自定義對象。 我在我的對象中創建了一個方法,使值大寫,但它不起作用。 小提琴
function mystring (name,uppercase){
this.name= name;
this.uppercase= function (){
return this.toUpperCase();
};
}
var jj= new mystring('mycompany');
jj=jj.uppercase();
console.log(jj)
你需要這樣做
function mystring (name,uppercase){
this.name= name;
this.uppercase= function (){
return this.name.toUpperCase();
};
}
var jj= new mystring('mycompany');
jj=jj.uppercase();
console.log(jj);
你忘了this.uppercase
函數中的this.name
您正在嘗試將整個對象轉換為大寫,如果您檢查控制台,它會告訴您該元素沒有方法toUpperCase
。 而是轉換字符串,而不是對象。
return this.name.toUpperCase();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.