Web Front-end Basics - Javascript API
Object API
Object.entries()
returns an array of a given object’s own enumerable string-keyed property[key, value]
pairs.Object.fromEntries()
method transforms a list of key-value pairs into an object.Object.prototype.valueOf()
method returns the primitive value of the specified object. The operator+
,-
,*
,/
with objects are acutally calling thevalueOf()
method under the hood to first convert objects into primitive values.
Property Descriptor
- value: current value of the property
- writable: boolean, decides whether the property can be assigned with a new value.
- enumerable: boolean, whether this property will show up in enumerations like
for in
loop orfor of
loop orObject.keys
,Object.assign()
etc. - configurable: boolean, whether it is allowed to change the property descriptor such as to change the value of
writable
andenumerable
settings.
Using Object.defineProperty(myObj, 'myProp', {})
gets a default property descriptor of:
1 | { |
Web APIs
eval()
: evaluates JavaScript code represented as a string.
Location Interface
window.location
or document.location
refers to the same interface.
location.href
represents the current url, it navigates to the new page if changed.location = "https://foo.bar"
is equal tolocation.href = "https://foo.bar"
and is equal tolocation.assign("https://foo.bar")
location.pathname
a USV string presents the path of the URL containing'/'
.location.search
a USV string represents the query string params containing'?'
.- URLSearchParams defines the interface for the parameters.
- URL.searchParams returns a
URLSearchParams
object allowing access to the GET decoded query arguments contained in the URL.
location.hash
a USV string represents the fragment identifier of the URL containing'#'
.location.replace()
will not be saved in session history, meaning the user won’t be able to use the back button to navigate it.location.toString()
returns a USV string containing the whole URL.location.reload(forceReload?: boolean)
: reloads the current URL, true to always load it from server.
Working with the History API
history.state
returns anany
value representing the state at the top of the history stack.history.back()
history.forward()
history.go(n: number)
:history.pushState(state: any, title?: string, URL?: string)
: pushes the given data onto the session history stackstate
: any Javasvript object that can be serialized, associated with the new history entry.title
:URL
: The new history entry’s URL, can be relative or absolute. The new URL must be of the same origin as the current URL, otherwisepushState()
will throw an exception. The browser won’t attempt to load this URL after a call topushState()
pushState()
never causes ahashchange
event to be fired, even if the new URL differs from the old URL only in its hash.
history.replaceState()
: updates the most recent entry on the history stack, it operates exactly likehistory.pushState()
, except that replaceState() modifies the current history entry instead of creating a new one.
A popstate
event is dispatched to the window every time the active history entry changes between two history entries for the same document.. If the history entry being activated was created by a call to pushState()
or affected by a call to replaceState()
, the popstate
event’s state
property contains a copy of the history entry’s state object.