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

attempt to define brand check #30

Merged
merged 7 commits into from
Nov 27, 2018
Merged
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions terminology.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,58 @@ name should be. We should avoid such bikeshedding.
#### Sources
[wikipedia](https://en.wiktionary.org/wiki/bikeshedding):

### Brand Check

#### Definition:

Brand check ("brand" as in a mark, or a brand made with a branding iron) is a term used by the TC39
rkirsling marked this conversation as resolved.
Show resolved Hide resolved
to describe a check against a unique datatype whose createion is controlled by a piece of code.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

spelling -- createion -> creation (typing too fast!)


#### Example:

One example of this is built in JavaScript datatypes, which are unique and cannot be made in user
space. For example, `toString` can be used on the `new Date` object, and this is a unique identifier
Copy link
Member

Choose a reason for hiding this comment

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

this example might not be great post-ES6, because of Symbol.toStringTag.

Array.isArray is a brand check, and Date.prototype.valueOf.call(date) will throw if date isn't a date object (iow it contains a brand check).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

super! thanks for the examples!

Copy link
Member

Choose a reason for hiding this comment

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

Yeah Array.isArray is a nice and more accessible example.

which returns `[object Date]`. For reference see [this
discussion](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3)
codehag marked this conversation as resolved.
Show resolved Hide resolved

However, this is not limited to datatypes that are implemented as a part of JavaScript. Brand checks
are possible in user space as long as there is a way to identify that the object is unique.

Imagine a library that implements dom queries and returns a `query` object. The author of this
Copy link
Contributor Author

Choose a reason for hiding this comment

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

dom -> DOM

library may be interested in being able to modify the implementation of the `query` object without
breaking the programs of users of the library. However, returning plain objects such as ` { type:
"queryResult", elements: [ ...... ] }` is not safe, as anyone can return such an object and create a
forgery of a `query` object. In order to avoid this, the library must make a brand check to ensure
that this object indeed belongs to the library. That can be done like so:

```javascript
const queries = new WeakMap();

class Query {
// ...

performQuery(queryString {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

missing paren

// returns the query object
return { type: "queryResult", elements: [ ...... ] };
}

get query(query) {
queries.get(query); // verifies that the query exists as a member of the WeakMap
}

set query(queryString) {
// generate a query object
const query = performQuery(queryString);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

missing a this.

// use the object itself as the key
queries.set(query, ...);
}
}
```


#### Sources
[esdiscuss comment](https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-3)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should link to forgery definition ?

### Temporal dead zone (TDZ)

#### Definition:
Expand Down