Intl.DateTimeFormat
の動作がわかりません。Intl.DateTimeFormatをJavaScriptオブジェクトとして理解する
JavaScriptオブジェクトから予想される動作は公開されません。 私はなぜそれを理解したいですか?
DateTimeFormat
のformat
メソッドをオーバーライドできないことを次のスニペットで示します。これはどのように可能ですか?また、原型継承を経てDateTimeFormat
由来
const itDateTimeFormat1 = new window.Intl.DateTimeFormat('it-CH');
const originalFormat = itDateTimeFormat1.format;
itDateTimeFormat1.format = function(date){ return 'Overriden! ' + originalFormat(date)};
console.log(itDateTimeFormat1.format(new Date())); // -> 13/7/2017
ことはできないようです。次のスニペットは、エラーがスローされます:
const itDateTimeFormat2 = new window.Intl.DateTimeFormat('it-CH');
const wrappedDateTimeFormat = Object.create(itDateTimeFormat2);
wrappedDateTimeFormat.format = function(date){ return 'Overriden! ' };
console.log(wrappedDateTimeFormat.format(new Date()));
// Firefox:
// TypeError: Intl.DateTimeFormat.prototype.format called on value that's not an object initialized as a DateTimeFormat
// Chrome:
// Uncaught TypeError: Method format called on incompatible receiver #<DateTimeFormat>
はなぜDateTimeFormat
「通常」のJavaScriptオブジェクトのように動作されていませんか?
DateTimeFormat
は、どのようにメソッドのオーバーライドを防止できますか?
DateTimeFormat
は派生オブジェクトのオーバーライドをどのように防ぐことができますか?
ありがとうございます。しかし、 'DateTimeFormat'の場合、派生オブジェクトは独自の' format'メソッドを提供できません(私は質問の2番目のスニペットを調整しました)。そんなことがあるものか? – jbandi
@jbandiは私に考えさせましょう:)私はこの振る舞いのスニペットも提供しようとします – Andrey
すばらしい例!ありがとう! – jbandi