Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Add notes for using @@toStringTag #2201

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions doc/api/util.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ Returns `true` if the given "object" is a `RegExp`. `false` otherwise.
util.isRegExp({})
// false

note: This function uses `Object.prototype.toString.call` and check the returned value is `[object RegExp]`. However, `Symbol.toStringTag` can change the returned value directly. `Symbol.toStringTag` is enabled under `--harmony-tostring`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Note:" (capitalize) and please wrap lines at 80 columns. I'd write 'enabled with' instead of 'enabled under'.


If the argument object is modified like below, this function does not work properly.

var fakeRegExp = {};
fakeRegExp[Symbol.toStringTag] = 'RegExp';
util.isRegExp(fakeRegExp);
// true but this is not object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"an object"


var regexp = /a/;
util.isRegExp(regexp);
// true
RegExp.prototype[Symbol.toStringTag] = 'Array';
util.isRegExp(regexp);
// false but this is RegExp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"a RegExp"


## util.isDate(object)

Expand All @@ -208,6 +223,9 @@ Returns `true` if the given "object" is a `Date`. `false` otherwise.
util.isDate({})
// false

note: This function uses `Object.prototype.toString.call` and check the returned value is `[object Date]`. However, `Symbol.toStringTag` can change the returned value directly.

see [an example in util.isRegExp](#util_util_isRegExp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize and wrap here and below.


## util.isError(object)

Expand All @@ -222,6 +240,9 @@ Returns `true` if the given "object" is an `Error`. `false` otherwise.
util.isError({ name: 'Error', message: 'an error occurred' })
// false

note: This function uses `Object.prototype.toString.call` and check the returned value is `[object Error]`. However, `Symbol.toStringTag` can change the returned value directly.

see [an example in util.isRegExp](#util_util_isRegExp)

## util.isBoolean(object)

Expand Down