問題2: 創(chuàng)建 “原生(native)” 方法
在 String
對象上定義一個 repeatify
函數(shù)。這個函數(shù)接受一個整數(shù)參數(shù),來明確字符串需要重復(fù)幾次。這個函數(shù)要求字符串重復(fù)指定的次數(shù)。舉個例子:
`console.log('hello'.repeatify(3));``
應(yīng)該打印出 hellohellohello
.
答案
一種可能的實現(xiàn)如下所示:
String.prototype.repeatify = String.prototype.repeatify || function(times) { var str = ''; for (var i = 0; i < times; i++) { str += this; } return str;};
這個問題測試了開發(fā)人員對于javascript中繼承的掌握,以及