string-replace.js 780 Bytes
Newer Older
1 2 3 4 5
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var pattern = {
6 7
  [Symbol.replace]: (string, newValue) => string + newValue,
  toString: () => "c"
8 9 10 11 12 13 14 15 16
};
// Check object coercible fails.
assertThrows(() => String.prototype.replace.call(null, pattern, "x"),
             TypeError);
// Override is called.
assertEquals("abcdex", "abcde".replace(pattern, "x"));
// Non-callable override.
pattern[Symbol.replace] = "dumdidum";
assertThrows(() => "abcde".replace(pattern, "x"), TypeError);
17 18 19
// Null override.
pattern[Symbol.replace] = null;
assertEquals("abXde", "abcde".replace(pattern, "X"));
20 21

assertEquals("[Symbol.replace]", RegExp.prototype[Symbol.replace].name);