包裹物件
先前有提過原始型別有各自的包裹物件,並且包裹物件有對應的 prototype
,如此以來也就能新增包裹物件中的方法屬性哩
1 2 3 4 5 6 7 8 9 10 11 12
| var name = 'Cloud'; var b = new String('bcde');
console.log(b); console.dir(String);
String.prototype.lastText = function() { return this[this.length - 1] };
console.log(b.lastText()); console.log(name.lastText());
|
1 2 3 4 5 6
| Number.prototype.secondPower = function() { return this * this; };
var num = 5; console.log(num.secondPower());
|
1 2 3 4 5 6 7 8 9 10 11 12
| var date = new Date(); console.log(date); console.dir(Date);
Date.prototype.getFullDate = function() { var dd = String(this.getDate()); var mm = String(this.getMonth() + 1); var yyyy = this.getFullYear(); var today = `${yyyy}/${mm}/${dd}`; return today; }; console.log(date.getFullDate());
|
參考資料
六角學院 - JavaScript 核心篇