mirror of
https://github.com/chubin/cheat.sh.git
synced 2024-11-25 23:46:00 +03:00
Merge branch 'master' into run-tests
This commit is contained in:
commit
18e851ce80
@ -1,4 +1,4 @@
|
||||
import re
|
||||
import search
|
||||
import fmt.comments
|
||||
|
||||
def postprocess(answer, keyword, options, request_options=None):
|
||||
@ -49,42 +49,8 @@ def _filter_by_keyword(answer, keyword, options):
|
||||
answer.append(paragraph)
|
||||
return answer
|
||||
|
||||
def _paragraph_contains(paragraph, keyword, insensitive=False, word_boundaries=True):
|
||||
"""
|
||||
Check if `paragraph` contains `keyword`.
|
||||
Several keywords can be joined together using ~
|
||||
For example: ~ssh~passphrase
|
||||
"""
|
||||
answer = True
|
||||
|
||||
if '~' in keyword:
|
||||
keywords = keyword.split('~')
|
||||
else:
|
||||
keywords = [keyword]
|
||||
|
||||
for kwrd in keywords:
|
||||
regex = re.escape(kwrd)
|
||||
if not word_boundaries:
|
||||
regex = r"\b%s\b" % kwrd
|
||||
|
||||
if insensitive:
|
||||
answer = answer and bool(re.search(regex, paragraph, re.IGNORECASE))
|
||||
else:
|
||||
answer = answer and bool(re.search(regex, paragraph))
|
||||
|
||||
return answer
|
||||
|
||||
|
||||
if not keyword:
|
||||
return answer
|
||||
|
||||
search_options = {
|
||||
'insensitive': 'i' in options,
|
||||
'word_boundaries': 'b' in options
|
||||
}
|
||||
|
||||
paragraphs = [p for p in _split_paragraphs(answer)
|
||||
if _paragraph_contains(p, keyword, **search_options)]
|
||||
if search.match(p, keyword, options=options)]
|
||||
if not paragraphs:
|
||||
return ""
|
||||
|
||||
|
@ -19,6 +19,8 @@ Configuration parameters:
|
||||
search.limit
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
from config import CONFIG
|
||||
from routing import get_answer_dict, get_topics_list
|
||||
|
||||
@ -30,13 +32,60 @@ def _limited_entry():
|
||||
'format': "code",
|
||||
}
|
||||
|
||||
def _parse_options(options):
|
||||
"""Parse search options string into optiond_dict
|
||||
"""
|
||||
|
||||
if options is None:
|
||||
return {}
|
||||
|
||||
search_options = {
|
||||
'insensitive': 'i' in options,
|
||||
'word_boundaries': 'b' in options,
|
||||
'recursive': 'r' in options,
|
||||
}
|
||||
return search_options
|
||||
|
||||
def match(paragraph, keyword, options=None, options_dict=None):
|
||||
"""Search for each keyword from `keywords` in `page`
|
||||
and if all of them are found, return `True`.
|
||||
Otherwise return `False`.
|
||||
|
||||
Several keywords can be joined together using ~
|
||||
For example: ~ssh~passphrase
|
||||
"""
|
||||
|
||||
if '~' in keyword:
|
||||
keywords = keyword.split('~')
|
||||
else:
|
||||
keywords = [keyword]
|
||||
|
||||
if options_dict is None:
|
||||
options_dict = _parse_options(options)
|
||||
|
||||
for kwrd in keywords:
|
||||
if not kwrd:
|
||||
continue
|
||||
|
||||
regex = re.escape(kwrd)
|
||||
if options_dict["word_boundaries"]:
|
||||
regex = r"\b%s\b" % kwrd
|
||||
|
||||
if options_dict["insensitive"]:
|
||||
if not re.search(regex, paragraph, re.IGNORECASE):
|
||||
return False
|
||||
else:
|
||||
if not re.search(regex, paragraph):
|
||||
return False
|
||||
return True
|
||||
|
||||
def find_answers_by_keyword(directory, keyword, options="", request_options=None):
|
||||
"""
|
||||
Search in the whole tree of all cheatsheets or in its subtree `directory`
|
||||
by `keyword`
|
||||
"""
|
||||
|
||||
recursive = 'r' in options
|
||||
options_dict = _parse_options(options)
|
||||
|
||||
answers_found = []
|
||||
for topic in get_topics_list(skip_internal=True, skip_dirs=True):
|
||||
@ -45,13 +94,13 @@ def find_answers_by_keyword(directory, keyword, options="", request_options=None
|
||||
continue
|
||||
|
||||
subtopic = topic[len(directory):]
|
||||
if not recursive and '/' in subtopic:
|
||||
if not options_dict["recursive"] and '/' in subtopic:
|
||||
continue
|
||||
|
||||
answer = get_answer_dict(topic, request_options=request_options)
|
||||
|
||||
if answer and answer.get('answer') and keyword.lower() in answer.get('answer', '').lower():
|
||||
answers_found.append(answer)
|
||||
answer_dict = get_answer_dict(topic, request_options=request_options)
|
||||
answer_text = answer_dict.get('answer', '')
|
||||
if match(answer_text, keyword, options_dict=options_dict):
|
||||
answers_found.append(answer_dict)
|
||||
|
||||
if len(answers_found) > CONFIG['search.limit']:
|
||||
answers_found.append(
|
||||
|
@ -24,8 +24,8 @@
|
||||
# count words in text counter
|
||||
# group elements list
|
||||
|
||||
__CHTSH_VERSION=6
|
||||
__CHTSH_DATETIME="2019-06-05 18:00:46 +0200"
|
||||
__CHTSH_VERSION=0.0.1
|
||||
__CHTSH_DATETIME="2020-08-05 09:30:30 +0200"
|
||||
|
||||
# cht.sh configuration loading
|
||||
#
|
||||
@ -80,6 +80,8 @@ cheatsh_standalone_install()
|
||||
local installdir; installdir="$1"
|
||||
local default_installdir="$HOME/.cheat.sh"
|
||||
|
||||
[ -z "$installdir" ] && installdir=${default_installdir}
|
||||
|
||||
if [ "$installdir" = help ]; then
|
||||
cat <<EOF
|
||||
Install cheat.sh in the standalone mode.
|
||||
@ -128,8 +130,9 @@ EOF
|
||||
[ "$_exit_code" -ne 0 ] && return "$_exit_code"
|
||||
|
||||
while true; do
|
||||
echo -n "Where should cheat.sh be installed [$default_installdir]? "; read -r installdir
|
||||
[ -n "$installdir" ] || installdir="$default_installdir"
|
||||
local _installdir
|
||||
echo -n "Where should cheat.sh be installed [$installdir]? "; read -r _installdir
|
||||
[ -n "$_installdir" ] && installdir=$_installdir
|
||||
|
||||
if [ "$installdir" = y ] \
|
||||
|| [ "$installdir" = Y ] \
|
||||
@ -172,8 +175,8 @@ EOF
|
||||
|
||||
# after the repository cloned, we may have the log directory
|
||||
# and we can write our installation log into it
|
||||
mkdir -p "$installdir/log/"
|
||||
LOG="$installdir/log/install.log"
|
||||
mkdir -p "log/"
|
||||
LOG="$PWD/log/install.log"
|
||||
|
||||
# we use tee everywhere so we should set -o pipefail
|
||||
set -o pipefail
|
||||
@ -184,7 +187,7 @@ EOF
|
||||
# set PYTHON2 to NO:
|
||||
# PYTHON2=NO
|
||||
#
|
||||
PYTHON2=YES
|
||||
PYTHON2=NO
|
||||
if [[ $PYTHON2 = YES ]]; then
|
||||
python="python2"
|
||||
pip="pip"
|
||||
@ -199,6 +202,8 @@ EOF
|
||||
"$python" "$(command -v virtualenv)" "${virtualenv_python3_option[@]}" ve \
|
||||
|| fatal Could not create virtual environment with "python2 $(command -v virtualenv) ve"
|
||||
|
||||
export CHEATSH_PATH_WORKDIR=$PWD
|
||||
|
||||
# rapidfuzz does not support Python 2,
|
||||
# so if we are using Python 2, install fuzzywuzzy instead
|
||||
if [[ $PYTHON2 = YES ]]; then
|
||||
|
@ -1,10 +1,12 @@
|
||||
1_Inheritance
|
||||
1line
|
||||
2_Multiple_Inheritance
|
||||
:learn
|
||||
:list
|
||||
Advanced
|
||||
Classes
|
||||
Comments
|
||||
Control_Flow
|
||||
Control_Flow_and_Iterables
|
||||
Functions
|
||||
Modules
|
||||
Primitive_Datatypes_and_Operators
|
||||
@ -13,6 +15,7 @@ doc
|
||||
func
|
||||
hello
|
||||
lambda
|
||||
list_comprehension
|
||||
loops
|
||||
recursion
|
||||
rosetta/
|
||||
|
1269
tests/results/15
1269
tests/results/15
File diff suppressed because it is too large
Load Diff
@ -2,15 +2,15 @@
|
||||
|
||||
1ns Main memory reference: Send 2,000 bytes Read 1,000,000 bytes
|
||||
[37m▗▖ [m100ns over commodity network: sequentially from SSD:
|
||||
[34m▗▖ [m62ns 61.712us
|
||||
[34m▗▖ [m44ns 48.981us
|
||||
L1 cache reference: 1ns [32m▗ [m[31m▗ [m
|
||||
[37m▗▖ [m1.0us
|
||||
[34m▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ [mSSD random read: 16.0us Disk seek: 2.679433ms
|
||||
Branch mispredict: 3ns [34m [m[32m▗▖▗ [m[31m▗▖▗▖▗ [m
|
||||
[34m▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ [mSSD random read: 16.0us Disk seek: 2.499999ms
|
||||
Branch mispredict: 3ns [34m [m[32m▗▖▗ [m[31m▗▖▗▖ [m
|
||||
[37m▗▖▗▖▗▖ [m
|
||||
Compress 1KB with Snappy: Read 1,000,000 bytes Read 1,000,000 bytes
|
||||
Compress 1KB wth Snappy: Read 1,000,000 bytes Read 1,000,000 bytes
|
||||
L2 cache reference: 4ns 2.0us sequentially from memory: sequentially from disk:
|
||||
[37m▗▖▗▖▗▖▗▖ [m[34m▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ [m3.738us 947.322us
|
||||
[37m▗▖▗▖▗▖▗▖ [m[34m▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ [m2.967us 824.692us
|
||||
[34m▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ [m[32m▗ [m[31m▗ [m
|
||||
Mutex lock/unlock: 16ns [34m [m
|
||||
[37m▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖▗▖ [m Round trip Packet roundtrip
|
||||
|
@ -1,33 +1,33 @@
|
||||
[38;5;246m# Microsoft Azure CLI 2.0[39m
|
||||
[38;5;246m# Command-line tools for Azure[39m
|
||||
[38;5;246;03m# Microsoft Azure CLI 2.0[39;00m
|
||||
[38;5;246;03m# Command-line tools for Azure[39;00m
|
||||
|
||||
[38;5;246m# Install Azure CLI 2.0 with one curl command.[39m
|
||||
[38;5;246;03m# Install Azure CLI 2.0 with one curl command.[39;00m
|
||||
[38;5;252mcurl[39m[38;5;252m [39m[38;5;252m-L[39m[38;5;252m [39m[38;5;252mhttps://aka.ms/InstallAzureCli[39m[38;5;252m [39m[38;5;252m|[39m[38;5;252m [39m[38;5;252mbash[39m
|
||||
|
||||
[38;5;246m# create a resource group named "MyResourceGroup" in the westus2 region of Azure[39m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mgroup[39m[38;5;252m [39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m-n[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m-l[39m[38;5;252m [39m[38;5;252mwestus2[39m[38;5;252m [39m
|
||||
[38;5;246;03m# create a resource group named "MyResourceGroup" in the westus2 region of Azure[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mgroup[39m[38;5;252m [39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m-n[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m-l[39m[38;5;252m [39m[38;5;252mwestus2[39m
|
||||
|
||||
[38;5;246m# create a Linux VM using the UbuntuTLS image, with two attached storage disks of 10 GB and 20 GB[39m
|
||||
[38;5;246;03m# create a Linux VM using the UbuntuTLS image, with two attached storage disks of 10 GB and 20 GB[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m-n[39m[38;5;252m [39m[38;5;252mMyLinuxVM[39m[38;5;252m [39m[38;5;252m-g[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m--ssh-key-value[39m[38;5;252m [39m[38;5;87m$HOME[39m[38;5;252m/.ssh/id_rsa.pub[39m[38;5;252m [39m[38;5;252m--image[39m[38;5;252m [39m[38;5;252mUbuntuLTS[39m[38;5;252m [39m[38;5;252m--data-disk-sizes-gb[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m [39m[38;5;67m20[39m
|
||||
|
||||
[38;5;246m# list VMs[39m
|
||||
[38;5;246;03m# list VMs[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# list only VMs having distinct state[39m
|
||||
[38;5;246;03m# list only VMs having distinct state[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m-d[39m[38;5;252m [39m[38;5;252m--query[39m[38;5;252m [39m[38;5;214m"[?powerState=='VM running']"[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# delete VM (with the name MyLinuxVM in the group MyResourceGroup)[39m
|
||||
[38;5;246;03m# delete VM (with the name MyLinuxVM in the group MyResourceGroup)[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mdelete[39m[38;5;252m [39m[38;5;252m-g[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m-n[39m[38;5;252m [39m[38;5;252mMyLinuxVM[39m[38;5;252m [39m[38;5;252m--yes[39m
|
||||
|
||||
[38;5;246m# Delete all VMs in a resource group[39m
|
||||
[38;5;246;03m# Delete all VMs in a resource group[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mdelete[39m[38;5;252m [39m[38;5;252m--ids[39m[38;5;252m [39m[38;5;70;01m$([39;00m[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m-g[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m--query[39m[38;5;252m [39m[38;5;214m"[].id"[39m[38;5;252m [39m[38;5;252m-o[39m[38;5;252m [39m[38;5;252mtsv[39m[38;5;70;01m)[39;00m
|
||||
|
||||
[38;5;246m# Create an Image based on a running VM[39m
|
||||
[38;5;246;03m# Create an Image based on a running VM[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mdeallocate[39m[38;5;252m [39m[38;5;252m-g[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m-n[39m[38;5;252m [39m[38;5;252mMyLinuxVM[39m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mgeneralize[39m[38;5;252m [39m[38;5;252m-g[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m-n[39m[38;5;252m [39m[38;5;252mMyLinuxVM[39m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mimage[39m[38;5;252m [39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m--resource-group[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m--name[39m[38;5;252m [39m[38;5;252mMyTestImage[39m[38;5;252m [39m[38;5;252m--source[39m[38;5;252m [39m[38;5;252mMyLinuxVM[39m
|
||||
|
||||
[38;5;246m# Running VM based on a VHD[39m
|
||||
[38;5;246;03m# Running VM based on a VHD[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mstorage[39m[38;5;252m [39m[38;5;252mblob[39m[38;5;252m [39m[38;5;252mupload[39m[38;5;252m [39m[38;5;252m--account-name[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214m${[39m[38;5;87maccount_name[39m[38;5;214m}[39m[38;5;214m"[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--account-key[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214m${[39m[38;5;87maccount_key[39m[38;5;214m}[39m[38;5;214m"[39m[38;5;252m [39m[38;5;252m--container-name[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214m${[39m[38;5;87mcontainer_name[39m[38;5;214m}[39m[38;5;214m"[39m[38;5;252m [39m[38;5;252m--type[39m[38;5;252m [39m[38;5;252mpage[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--file[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214m${[39m[38;5;87mfile[39m[38;5;214m}[39m[38;5;214m"[39m[38;5;252m [39m[38;5;252m--name[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214m${[39m[38;5;87mvhd_name[39m[38;5;214m}[39m[38;5;214m"[39m
|
||||
@ -36,55 +36,55 @@
|
||||
[38;5;252m [39m[38;5;252m--name[39m[38;5;252m [39m[38;5;252mmyManagedDisk[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--source[39m[38;5;252m [39m[38;5;252mhttps://[39m[38;5;214m${[39m[38;5;87maccount_name[39m[38;5;214m}[39m[38;5;252m.blob.core.windows.net/[39m[38;5;214m${[39m[38;5;87mcontainer_name[39m[38;5;214m}[39m[38;5;252m/[39m[38;5;214m${[39m[38;5;87mvhd_name[39m[38;5;214m}[39m
|
||||
|
||||
[38;5;246m# open port[39m
|
||||
[38;5;246;03m# open port[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mopen-port[39m[38;5;252m [39m[38;5;252m--resource-group[39m[38;5;252m [39m[38;5;252mMyResourceGroup[39m[38;5;252m [39m[38;5;252m--name[39m[38;5;252m [39m[38;5;252mMyLinuxVM[39m[38;5;252m [39m[38;5;252m--port[39m[38;5;252m [39m[38;5;67m443[39m[38;5;252m [39m[38;5;252m--priority[39m[38;5;252m [39m[38;5;67m899[39m
|
||||
|
||||
[38;5;246m# Show storage accounts[39m
|
||||
[38;5;246;03m# Show storage accounts[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mstorage[39m[38;5;252m [39m[38;5;252maccount[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# Show contaniers for an account[39m
|
||||
[38;5;246;03m# Show contaniers for an account[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mstorage[39m[38;5;252m [39m[38;5;252mcontainer[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--account-name[39m[38;5;252m [39m[38;5;252mmystorageaccount[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# Show blobs in a container[39m
|
||||
[38;5;246;03m# Show blobs in a container[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mstorage[39m[38;5;252m [39m[38;5;252mblob[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--account-name[39m[38;5;252m [39m[38;5;252mmystorageaccount[39m[38;5;252m [39m[38;5;252m--container-name[39m[38;5;252m [39m[38;5;252mmycontainer[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# list account keys[39m
|
||||
[38;5;246;03m# list account keys[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mstorage[39m[38;5;252m [39m[38;5;252maccount[39m[38;5;252m [39m[38;5;252mkeys[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--account-name[39m[38;5;252m [39m[38;5;252mSTORAGE_NAME[39m[38;5;252m [39m[38;5;252m--resource-group[39m[38;5;252m [39m[38;5;252mRESOURCE_GROUP[39m
|
||||
|
||||
[38;5;246m# Show own images[39m
|
||||
[38;5;246;03m# Show own images[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mimage[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# Configure default storage location[39m
|
||||
[38;5;246;03m# Configure default storage location[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mconfigure[39m[38;5;252m [39m[38;5;252m--defaults[39m[38;5;252m [39m[38;5;87mlocation[39m[38;5;252m=[39m[38;5;252meastus2[39m
|
||||
|
||||
[38;5;246m# Show disks[39m
|
||||
[38;5;246;03m# Show disks[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mdisk[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# Copy blob[39m
|
||||
[38;5;246;03m# Copy blob[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mstorage[39m[38;5;252m [39m[38;5;252mblob[39m[38;5;252m [39m[38;5;252mcopy[39m[38;5;252m [39m[38;5;252mstart[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--source-uri[39m[38;5;252m [39m[38;5;214m'https://md-ldh5nknx2rkz.blob.core.windows.net/jzwuuuzzapn0/abcd?sv=2017-04-17&sr=b&si=68041718-6828-4f5e-9e6e-a1b719975062&sig=XXX'[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--account-key[39m[38;5;252m [39m[38;5;87mXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX[39m[38;5;252m=[39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--account-name[39m[38;5;252m [39m[38;5;252mdestaccount[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--destination-container[39m[38;5;252m [39m[38;5;252mvms[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--destination-blob[39m[38;5;252m [39m[38;5;252mDESTINATION-blob.vhd[39m
|
||||
[38;5;252m [39m[38;5;252m--source-uri[39m[38;5;252m [39m[38;5;214m'https://md-ldh5nknx2rkz.blob.core.windows.net/jzwuuuzzapn0/abcd?sv=2017-04-17&sr=b&si=68041718-6828-4f5e-9e6e-a1b719975062&sig=XXX'[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--account-key[39m[38;5;252m [39m[38;5;87mXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX[39m[38;5;252m=[39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--account-name[39m[38;5;252m [39m[38;5;252mdestaccount[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--destination-container[39m[38;5;252m [39m[38;5;252mvms[39m[38;5;252m [39m[38;5;214m\[39m
|
||||
[38;5;252m [39m[38;5;252m--destination-blob[39m[38;5;252m [39m[38;5;252mDESTINATION-blob.vhd[39m
|
||||
|
||||
[38;5;246m# List virtual networks[39m
|
||||
[38;5;246;03m# List virtual networks[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mnetwork[39m[38;5;252m [39m[38;5;252mvnet[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# List virtual networks adapters[39m
|
||||
[38;5;246;03m# List virtual networks adapters[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mnetwork[39m[38;5;252m [39m[38;5;252mnic[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# List public IP addresses used by the VMs[39m
|
||||
[38;5;246;03m# List public IP addresses used by the VMs[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mlist-ip-addresses[39m[38;5;252m [39m[38;5;252m--output[39m[38;5;252m [39m[38;5;252mtable[39m
|
||||
|
||||
[38;5;246m# create snapshot[39m
|
||||
[38;5;246;03m# create snapshot[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252msnapshot[39m[38;5;252m [39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m--resource-group[39m[38;5;252m [39m[38;5;252mIC-EXASOL-001[39m[38;5;252m [39m[38;5;252m--source[39m[38;5;252m [39m[38;5;252mvm1-disk1[39m[38;5;252m [39m[38;5;252m-n[39m[38;5;252m [39m[38;5;252mvm1-snap1[39m
|
||||
|
||||
[38;5;246m# create SAS url for a snapshot[39m
|
||||
[38;5;246;03m# create SAS url for a snapshot[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252msnapshot[39m[38;5;252m [39m[38;5;252mgrant-access[39m[38;5;252m [39m[38;5;252m--resource-group[39m[38;5;252m [39m[38;5;252mIC-EXASOL-001[39m[38;5;252m [39m[38;5;252m--name[39m[38;5;252m [39m[38;5;252mvm1-snap1[39m[38;5;252m [39m[38;5;252m--duration-in-seconds[39m[38;5;252m [39m[38;5;67m36000[39m[38;5;252m [39m[38;5;252m--query[39m[38;5;252m [39m[38;5;214m'[accessSas]'[39m[38;5;252m [39m[38;5;252m-o[39m[38;5;252m [39m[38;5;252mtsv[39m
|
||||
|
||||
[38;5;246m# attach disk[39m
|
||||
[38;5;246;03m# attach disk[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mdisk[39m[38;5;252m [39m[38;5;252mattach[39m[38;5;252m [39m[38;5;252m--vm-name[39m[38;5;252m [39m[38;5;252mvm1[39m[38;5;252m [39m[38;5;252m-g[39m[38;5;252m [39m[38;5;252mRESOURCE_GROUP[39m[38;5;252m [39m[38;5;252m--disk[39m[38;5;252m [39m[38;5;252mDISK1_ID[39m
|
||||
|
||||
[38;5;246m# detach disk[39m
|
||||
[38;5;246;03m# detach disk[39;00m
|
||||
[38;5;252maz[39m[38;5;252m [39m[38;5;252mvm[39m[38;5;252m [39m[38;5;252mdisk[39m[38;5;252m [39m[38;5;252mdetach[39m[38;5;252m [39m[38;5;252m--vm-name[39m[38;5;252m [39m[38;5;252mvm1[39m[38;5;252m [39m[38;5;252m-g[39m[38;5;252m [39m[38;5;252mRESOURCE_GROUP[39m[38;5;252m [39m[38;5;252m--name[39m[38;5;252m [39m[38;5;252mDISK1_ID[39m
|
||||
|
@ -1,19 +1,19 @@
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mabcdefgh[39m[38;5;214m'[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252mn[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mm[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mchar[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mchars[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214md[39m[38;5;214m'[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m'[39m[38;5;214mcd[39m[38;5;214m'[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246m# starting from n=2 characters in and m=3 in length;[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246;03m# starting from n=2 characters in and m=3 in length;[39;00m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m[[39m[38;5;252mn[39m[38;5;252m-[39m[38;5;67m1[39m[38;5;252m:[39m[38;5;252mn[39m[38;5;252m+[39m[38;5;252mm[39m[38;5;252m-[39m[38;5;67m1[39m[38;5;252m][39m
|
||||
[38;5;214m'[39m[38;5;214mbcd[39m[38;5;214m'[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246m# starting from n characters in, up to the end of the string;[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246;03m# starting from n characters in, up to the end of the string;[39;00m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m[[39m[38;5;252mn[39m[38;5;252m-[39m[38;5;67m1[39m[38;5;252m:[39m[38;5;252m][39m
|
||||
[38;5;214m'[39m[38;5;214mbcdefgh[39m[38;5;214m'[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246m# whole string minus last character;[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246;03m# whole string minus last character;[39;00m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m[[39m[38;5;252m:[39m[38;5;252m-[39m[38;5;67m1[39m[38;5;252m][39m
|
||||
[38;5;214m'[39m[38;5;214mabcdefg[39m[38;5;214m'[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246m# starting from a known character char="d" within the string and of m length;[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246;03m# starting from a known character char="d" within the string and of m length;[39;00m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252mindx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m.[39m[38;5;252mindex[39m[38;5;252m([39m[38;5;252mchar[39m[38;5;252m)[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m[[39m[38;5;252mindx[39m[38;5;252m:[39m[38;5;252mindx[39m[38;5;252m+[39m[38;5;252mm[39m[38;5;252m][39m
|
||||
[38;5;214m'[39m[38;5;214mdef[39m[38;5;214m'[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246m# starting from a known substring chars="cd" within the string and of m length.[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;246;03m# starting from a known substring chars="cd" within the string and of m length.[39;00m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252mindx[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m.[39m[38;5;252mindex[39m[38;5;252m([39m[38;5;252mchars[39m[38;5;252m)[39m
|
||||
[38;5;252m>>[39m[38;5;252m>[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m[[39m[38;5;252mindx[39m[38;5;252m:[39m[38;5;252mindx[39m[38;5;252m+[39m[38;5;252mm[39m[38;5;252m][39m
|
||||
[38;5;214m'[39m[38;5;214mcde[39m[38;5;214m'[39m
|
||||
|
@ -1,17 +1,17 @@
|
||||
[38;5;246m# Displays everything in the target directory[39m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252mpath/to/the/target/directory[39m
|
||||
[38;5;246;03m# To display everything in <dir>, excluding hidden files:[39;00m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252mdir>[39m
|
||||
|
||||
[38;5;246m# Displays everything including hidden files[39m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-a[39m
|
||||
[38;5;246;03m# To display everything in <dir>, including hidden files:[39;00m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-a[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252mdir>[39m
|
||||
|
||||
[38;5;246m# Displays all files, along with the size (with unit suffixes) and timestamp[39m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-lh[39m[38;5;252m [39m
|
||||
[38;5;246;03m# To display all files, along with the size (with unit suffixes) and timestamp[39;00m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-lh[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252mdir>[39m
|
||||
|
||||
[38;5;246m# Display files, sorted by size[39m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-S[39m
|
||||
[38;5;246;03m# To display files, sorted by size:[39;00m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-S[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252mdir>[39m
|
||||
|
||||
[38;5;246m# Display directories only[39m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-d[39m[38;5;252m [39m[38;5;252m*/[39m
|
||||
[38;5;246;03m# To display directories only:[39;00m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-d[39m[38;5;252m [39m[38;5;252m*/[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252mdir>[39m
|
||||
|
||||
[38;5;246m# Display directories only, include hidden[39m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-d[39m[38;5;252m [39m[38;5;252m.*/[39m[38;5;252m [39m[38;5;252m*/[39m
|
||||
[38;5;246;03m# To display directories only, include hidden:[39;00m
|
||||
[38;5;252mls[39m[38;5;252m [39m[38;5;252m-d[39m[38;5;252m [39m[38;5;252m.*/[39m[38;5;252m [39m[38;5;252m*/[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252mdir>[39m
|
||||
|
@ -65,7 +65,9 @@ Bitmap-Bresenhams-line-algorithm
|
||||
Bitmap-Flood-fill
|
||||
Bitmap-Histogram
|
||||
Bitmap-Midpoint-circle-algorithm
|
||||
Bitmap-PPM-conversion-through-a-pipe
|
||||
Bitmap-Read-a-PPM-file
|
||||
Bitmap-Read-an-image-through-a-pipe
|
||||
Bitmap-Write-a-PPM-file
|
||||
Bitwise-IO
|
||||
Bitwise-operations
|
||||
@ -92,10 +94,12 @@ Casting-out-nines
|
||||
Catalan-numbers
|
||||
Catalan-numbers-Pascals-triangle
|
||||
Catamorphism
|
||||
Catmull-Clark-subdivision-surface
|
||||
Character-codes
|
||||
Chat-server
|
||||
Check-Machin-like-formulas
|
||||
Check-that-file-exists
|
||||
Checkpoint-synchronization
|
||||
Chinese-remainder-theorem
|
||||
Cholesky-decomposition
|
||||
Circles-of-given-radius-through-two-points
|
||||
@ -107,6 +111,7 @@ Color-of-a-screen-pixel
|
||||
Color-quantization
|
||||
Colour-bars-Display
|
||||
Colour-pinstripe-Display
|
||||
Colour-pinstripe-Printer
|
||||
Combinations
|
||||
Combinations-and-permutations
|
||||
Combinations-with-repetitions
|
||||
@ -141,6 +146,7 @@ Day-of-the-week
|
||||
Deal-cards-for-FreeCell
|
||||
Death-Star
|
||||
Deconvolution-1D
|
||||
Deconvolution-2D+
|
||||
Deepcopy
|
||||
Define-a-primitive-data-type
|
||||
Delegates
|
||||
@ -689,6 +695,7 @@ Variadic-function
|
||||
Vector-products
|
||||
Verify-distribution-uniformity-Chi-squared-test
|
||||
Verify-distribution-uniformity-Naive
|
||||
Video-display-modes
|
||||
Vigen-re-cipher
|
||||
Vigen-re-cipher-Cryptanalysis
|
||||
Visualize-a-tree
|
||||
@ -715,6 +722,7 @@ Y-combinator
|
||||
Yahoo--search-interface
|
||||
Yin-and-yang
|
||||
Zebra-puzzle
|
||||
Zeckendorf-arithmetic
|
||||
Zeckendorf-number-representation
|
||||
Zero-to-the-zero-power
|
||||
Zhang-Suen-thinning-algorithm
|
||||
|
630
tests/results/21
630
tests/results/21
@ -1,270 +1,270 @@
|
||||
[38;5;246m// Single-line comments start with two slashes.[39m
|
||||
[38;5;246m/* Multiline comments start with slash-star,[39m
|
||||
[38;5;246m and end with star-slash */[39m
|
||||
[38;5;246;03m// Single-line comments start with two slashes.[39;00m
|
||||
[38;5;246;03m/* Multiline comments start with slash-star,[39;00m
|
||||
[38;5;246;03m and end with star-slash */[39;00m
|
||||
|
||||
[38;5;246m// Statements can be terminated by ;[39m
|
||||
[38;5;246;03m// Statements can be terminated by ;[39;00m
|
||||
[38;5;252mdoStuff[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// ... but they don't have to be, as semicolons are automatically inserted[39m
|
||||
[38;5;246m// wherever there's a newline, except in certain cases.[39m
|
||||
[38;5;246;03m// ... but they don't have to be, as semicolons are automatically inserted[39;00m
|
||||
[38;5;246;03m// wherever there's a newline, except in certain cases.[39;00m
|
||||
[38;5;252mdoStuff[39m[38;5;252m([39m[38;5;252m)[39m
|
||||
|
||||
[38;5;246m// Because those cases can cause unexpected results, we'll keep on using[39m
|
||||
[38;5;246m// semicolons in this guide.[39m
|
||||
[38;5;246;03m// Because those cases can cause unexpected results, we'll keep on using[39;00m
|
||||
[38;5;246;03m// semicolons in this guide.[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 1. Numbers, Strings and Operators[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 1. Numbers, Strings and Operators[39;00m
|
||||
|
||||
[38;5;246m// JavaScript has one number type (which is a 64-bit IEEE 754 double).[39m
|
||||
[38;5;246m// Doubles have a 52-bit mantissa, which is enough to store integers[39m
|
||||
[38;5;246m// up to about 9✕10¹⁵ precisely.[39m
|
||||
[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 3[39m
|
||||
[38;5;67m1.5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 1.5[39m
|
||||
[38;5;246;03m// JavaScript has one number type (which is a 64-bit IEEE 754 double).[39;00m
|
||||
[38;5;246;03m// Doubles have a 52-bit mantissa, which is enough to store integers[39;00m
|
||||
[38;5;246;03m// up to about 9✕10¹⁵ precisely.[39;00m
|
||||
[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 3[39;00m
|
||||
[38;5;67m1.5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 1.5[39;00m
|
||||
|
||||
[38;5;246m// Some basic arithmetic works as you'd expect.[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 2[39m
|
||||
[38;5;67m0.1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m0.2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 0.30000000000000004[39m
|
||||
[38;5;67m8[39m[38;5;252m [39m[38;5;252m-[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 7[39m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 20[39m
|
||||
[38;5;67m35[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 7[39m
|
||||
[38;5;246;03m// Some basic arithmetic works as you'd expect.[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 2[39;00m
|
||||
[38;5;67m0.1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m0.2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 0.30000000000000004[39;00m
|
||||
[38;5;67m8[39m[38;5;252m [39m[38;5;252m-[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 7[39;00m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 20[39;00m
|
||||
[38;5;67m35[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 7[39;00m
|
||||
|
||||
[38;5;246m// Including uneven division.[39m
|
||||
[38;5;67m5[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 2.5[39m
|
||||
[38;5;246;03m// Including uneven division.[39;00m
|
||||
[38;5;67m5[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 2.5[39;00m
|
||||
|
||||
[38;5;246m// And modulo division.[39m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 0[39m
|
||||
[38;5;67m30[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 2[39m
|
||||
[38;5;67m18.5[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m7[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4.5[39m
|
||||
[38;5;246;03m// And modulo division.[39;00m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 0[39;00m
|
||||
[38;5;67m30[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 2[39;00m
|
||||
[38;5;67m18.5[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m7[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4.5[39;00m
|
||||
|
||||
[38;5;246m// Bitwise operations also work; when you perform a bitwise operation your float[39m
|
||||
[38;5;246m// is converted to a signed int *up to* 32 bits.[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4[39m
|
||||
[38;5;246;03m// Bitwise operations also work; when you perform a bitwise operation your float[39;00m
|
||||
[38;5;246;03m// is converted to a signed int *up to* 32 bits.[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4[39;00m
|
||||
|
||||
[38;5;246m// Precedence is enforced with parentheses.[39m
|
||||
[38;5;252m([39m[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 8[39m
|
||||
[38;5;246;03m// Precedence is enforced with parentheses.[39;00m
|
||||
[38;5;252m([39m[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 8[39;00m
|
||||
|
||||
[38;5;246m// There are three special not-a-real-number values:[39m
|
||||
[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// result of e.g. 1/0[39m
|
||||
[38;5;252m-[39m[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// result of e.g. -1/0[39m
|
||||
[38;5;70;01mNaN[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// result of e.g. 0/0, stands for 'Not a Number'[39m
|
||||
[38;5;246;03m// There are three special not-a-real-number values:[39;00m
|
||||
[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// result of e.g. 1/0[39;00m
|
||||
[38;5;252m-[39m[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// result of e.g. -1/0[39;00m
|
||||
[38;5;70;01mNaN[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// result of e.g. 0/0, stands for 'Not a Number'[39;00m
|
||||
|
||||
[38;5;246m// There's also a boolean type.[39m
|
||||
[38;5;246;03m// There's also a boolean type.[39;00m
|
||||
[38;5;70;01mtrue[39;00m[38;5;252m;[39m
|
||||
[38;5;70;01mfalse[39;00m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Strings are created with ' or ".[39m
|
||||
[38;5;246;03m// Strings are created with ' or ".[39;00m
|
||||
[38;5;214m'abc'[39m[38;5;252m;[39m
|
||||
[38;5;214m"Hello, world"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Negation uses the ! symbol[39m
|
||||
[38;5;252m![39m[38;5;70;01mtrue[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;252m![39m[38;5;70;01mfalse[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// Negation uses the ! symbol[39;00m
|
||||
[38;5;252m![39m[38;5;70;01mtrue[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;252m![39m[38;5;70;01mfalse[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Equality is ===[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;246;03m// Equality is ===[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
|
||||
[38;5;246m// Inequality is !==[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// Inequality is !==[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// More comparisons[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m<=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m>=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// More comparisons[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m<=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m>=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Strings are concatenated with +[39m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"world!"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world!"[39m
|
||||
[38;5;246;03m// Strings are concatenated with +[39;00m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"world!"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world!"[39;00m
|
||||
|
||||
[38;5;246m// ... which works with more than just strings[39m
|
||||
[38;5;214m"1, 2, "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "1, 2, 3"[39m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"world"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"!"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world,!"[39m
|
||||
[38;5;246;03m// ... which works with more than just strings[39;00m
|
||||
[38;5;214m"1, 2, "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "1, 2, 3"[39;00m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"world"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"!"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world,!"[39;00m
|
||||
|
||||
[38;5;246m// and are compared with < and >[39m
|
||||
[38;5;214m"a"[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;214m"b"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// and are compared with < and >[39;00m
|
||||
[38;5;214m"a"[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;214m"b"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Type coercion is performed for comparisons with double equals...[39m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// Type coercion is performed for comparisons with double equals...[39;00m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// ...unless you use ===[39m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;246;03m// ...unless you use ===[39;00m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
|
||||
[38;5;246m// ...which can result in some weird behaviour...[39m
|
||||
[38;5;67m13[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// 14[39m
|
||||
[38;5;214m"13"[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// '13true'[39m
|
||||
[38;5;246;03m// ...which can result in some weird behaviour...[39;00m
|
||||
[38;5;67m13[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// 14[39;00m
|
||||
[38;5;214m"13"[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// '13true'[39;00m
|
||||
|
||||
[38;5;246m// You can access characters in a string with `charAt`[39m
|
||||
[38;5;214m"This is a string"[39m[38;5;252m.[39m[38;5;252mcharAt[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 'T'[39m
|
||||
[38;5;246;03m// You can access characters in a string with `charAt`[39;00m
|
||||
[38;5;214m"This is a string"[39m[38;5;252m.[39m[38;5;252mcharAt[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 'T'[39;00m
|
||||
|
||||
[38;5;246m// ...or use `substring` to get larger pieces.[39m
|
||||
[38;5;214m"Hello world"[39m[38;5;252m.[39m[38;5;252msubstring[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello"[39m
|
||||
[38;5;246;03m// ...or use `substring` to get larger pieces.[39;00m
|
||||
[38;5;214m"Hello world"[39m[38;5;252m.[39m[38;5;252msubstring[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello"[39;00m
|
||||
|
||||
[38;5;246m// `length` is a property, so don't use ().[39m
|
||||
[38;5;214m"Hello"[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5[39m
|
||||
[38;5;246;03m// `length` is a property, so don't use ().[39;00m
|
||||
[38;5;214m"Hello"[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5[39;00m
|
||||
|
||||
[38;5;246m// There's also `null` and `undefined`.[39m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// used to indicate a deliberate non-value[39m
|
||||
[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// used to indicate a value is not currently present (although[39m
|
||||
[38;5;252m [39m[38;5;246m// `undefined` is actually a value itself)[39m
|
||||
[38;5;246;03m// There's also `null` and `undefined`.[39;00m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// used to indicate a deliberate non-value[39;00m
|
||||
[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// used to indicate a value is not currently present (although[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// `undefined` is actually a value itself)[39;00m
|
||||
|
||||
[38;5;246m// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.[39m
|
||||
[38;5;246m// Note that 0 is falsy and "0" is truthy, even though 0 == "0".[39m
|
||||
[38;5;246;03m// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.[39;00m
|
||||
[38;5;246;03m// Note that 0 is falsy and "0" is truthy, even though 0 == "0".[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 2. Variables, Arrays and Objects[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 2. Variables, Arrays and Objects[39;00m
|
||||
|
||||
[38;5;246m// Variables are declared with the `var` keyword. JavaScript is dynamically[39m
|
||||
[38;5;246m// typed, so you don't need to specify type. Assignment uses a single `=`[39m
|
||||
[38;5;246m// character.[39m
|
||||
[38;5;246;03m// Variables are declared with the `var` keyword. JavaScript is dynamically[39;00m
|
||||
[38;5;246;03m// typed, so you don't need to specify type. Assignment uses a single `=`[39;00m
|
||||
[38;5;246;03m// character.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// If you leave the var keyword off, you won't get an error...[39m
|
||||
[38;5;246;03m// If you leave the var keyword off, you won't get an error...[39;00m
|
||||
[38;5;252msomeOtherVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// ...but your variable will be created in the global scope, not in the scope[39m
|
||||
[38;5;246m// you defined it in.[39m
|
||||
[38;5;246;03m// ...but your variable will be created in the global scope, not in the scope[39;00m
|
||||
[38;5;246;03m// you defined it in.[39;00m
|
||||
|
||||
[38;5;246m// Variables declared without being assigned to are set to undefined.[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeThirdVar[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;246;03m// Variables declared without being assigned to are set to undefined.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeThirdVar[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m// If you want to declare a couple of variables, then you could use a comma[39m
|
||||
[38;5;246m// separator[39m
|
||||
[38;5;246;03m// If you want to declare a couple of variables, then you could use a comma[39;00m
|
||||
[38;5;246;03m// separator[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeFourthVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252msomeFifthVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// There's shorthand for performing math operations on variables:[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// equivalent to someVar = someVar + 5; someVar is 10 now[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m*=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// now someVar is 100[39m
|
||||
[38;5;246;03m// There's shorthand for performing math operations on variables:[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// equivalent to someVar = someVar + 5; someVar is 10 now[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m*=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// now someVar is 100[39;00m
|
||||
|
||||
[38;5;246m// and an even-shorter-hand for adding or subtracting 1[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m++[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// now someVar is 101[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m--[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// back to 100[39m
|
||||
[38;5;246;03m// and an even-shorter-hand for adding or subtracting 1[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m++[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// now someVar is 101[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m--[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// back to 100[39;00m
|
||||
|
||||
[38;5;246m// Arrays are ordered lists of values, of any type.[39m
|
||||
[38;5;246;03m// Arrays are ordered lists of values, of any type.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"Hello"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m45[39m[38;5;252m,[39m[38;5;252m [39m[38;5;70;01mtrue[39;00m[38;5;252m][39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Their members can be accessed using the square-brackets subscript syntax.[39m
|
||||
[38;5;246m// Array indices start at zero.[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 45[39m
|
||||
[38;5;246;03m// Their members can be accessed using the square-brackets subscript syntax.[39;00m
|
||||
[38;5;246;03m// Array indices start at zero.[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 45[39;00m
|
||||
|
||||
[38;5;246m// Arrays are mutable and of variable length.[39m
|
||||
[38;5;246;03m// Arrays are mutable and of variable length.[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpush[39m[38;5;252m([39m[38;5;214m"World"[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4[39;00m
|
||||
|
||||
[38;5;246m// Add/Modify at specific index[39m
|
||||
[38;5;246;03m// Add/Modify at specific index[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m[[39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"Hello"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Add and remove element from front or back end of an array[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252munshift[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Add as the first element[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mshift[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Remove first element and return it[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpush[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Add as the last element[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpop[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Remove last element and return it[39m
|
||||
[38;5;246;03m// Add and remove element from front or back end of an array[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252munshift[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Add as the first element[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mshift[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Remove first element and return it[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpush[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Add as the last element[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpop[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Remove last element and return it[39;00m
|
||||
|
||||
[38;5;246m// Join all elements of an array with semicolon[39m
|
||||
[38;5;246;03m// Join all elements of an array with semicolon[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyArray0[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m32[39m[38;5;252m,[39m[38;5;70;01mfalse[39;00m[38;5;252m,[39m[38;5;214m"js"[39m[38;5;252m,[39m[38;5;67m12[39m[38;5;252m,[39m[38;5;67m56[39m[38;5;252m,[39m[38;5;67m90[39m[38;5;252m][39m[38;5;252m;[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mjoin[39m[38;5;252m([39m[38;5;214m";"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246m// = "32;false;js;12;56;90"[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mjoin[39m[38;5;252m([39m[38;5;214m";"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "32;false;js;12;56;90"[39;00m
|
||||
|
||||
[38;5;246m// Get subarray of elements from index 1 (include) to 4 (exclude)[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mslice[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = [false,"js",12][39m
|
||||
[38;5;246;03m// Get subarray of elements from index 1 (include) to 4 (exclude)[39;00m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mslice[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = [false,"js",12][39;00m
|
||||
|
||||
[38;5;246m// Remove 4 elements starting from index 2, and insert there strings[39m
|
||||
[38;5;246m// "hi","wr" and "ld"; return removed subarray[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252msplice[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;214m"hi"[39m[38;5;252m,[39m[38;5;214m"wr"[39m[38;5;252m,[39m[38;5;214m"ld"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = ["js",12,56,90][39m
|
||||
[38;5;246m// myArray0 === [32,false,"hi","wr","ld"][39m
|
||||
[38;5;246;03m// Remove 4 elements starting from index 2, and insert there strings[39;00m
|
||||
[38;5;246;03m// "hi","wr" and "ld"; return removed subarray[39;00m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252msplice[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;214m"hi"[39m[38;5;252m,[39m[38;5;214m"wr"[39m[38;5;252m,[39m[38;5;214m"ld"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = ["js",12,56,90][39;00m
|
||||
[38;5;246;03m// myArray0 === [32,false,"hi","wr","ld"][39;00m
|
||||
|
||||
[38;5;246m// JavaScript's objects are equivalent to "dictionaries" or "maps" in other[39m
|
||||
[38;5;246m// languages: an unordered collection of key-value pairs.[39m
|
||||
[38;5;246;03m// JavaScript's objects are equivalent to "dictionaries" or "maps" in other[39;00m
|
||||
[38;5;246;03m// languages: an unordered collection of key-value pairs.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252mkey1[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"Hello"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mkey2[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"World"[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Keys are strings, but quotes aren't required if they're a valid[39m
|
||||
[38;5;246m// JavaScript identifier. Values can be any type.[39m
|
||||
[38;5;246;03m// Keys are strings, but quotes aren't required if they're a valid[39;00m
|
||||
[38;5;246;03m// JavaScript identifier. Values can be any type.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252mmyKey[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"myValue"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"my other key"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Object attributes can also be accessed using the subscript syntax,[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;214m"my other key"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4[39m
|
||||
[38;5;246;03m// Object attributes can also be accessed using the subscript syntax,[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;214m"my other key"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4[39;00m
|
||||
|
||||
[38;5;246m// ... or using the dot syntax, provided the key is a valid identifier.[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "myValue"[39m
|
||||
[38;5;246;03m// ... or using the dot syntax, provided the key is a valid identifier.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "myValue"[39;00m
|
||||
|
||||
[38;5;246m// Objects are mutable; values can be changed and new keys added.[39m
|
||||
[38;5;246;03m// Objects are mutable; values can be changed and new keys added.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyThirdKey[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mtrue[39;00m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// If you try to access a value that's not yet set, you'll get undefined.[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFourthKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;246;03m// If you try to access a value that's not yet set, you'll get undefined.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFourthKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 3. Logic and Control Structures[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 3. Logic and Control Structures[39;00m
|
||||
|
||||
[38;5;246m// The `if` structure works as you'd expect.[39m
|
||||
[38;5;246;03m// The `if` structure works as you'd expect.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mcount[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mcount[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// evaluated if count is 3[39m
|
||||
[38;5;252m [39m[38;5;246;03m// evaluated if count is 3[39;00m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;70;01melse[39;00m[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mcount[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// evaluated if count is 4[39m
|
||||
[38;5;252m [39m[38;5;246;03m// evaluated if count is 4[39;00m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;70;01melse[39;00m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// evaluated if it's not either 3 or 4[39m
|
||||
[38;5;252m [39m[38;5;246;03m// evaluated if it's not either 3 or 4[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// As does `while`.[39m
|
||||
[38;5;246;03m// As does `while`.[39;00m
|
||||
[38;5;70;01mwhile[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mtrue[39;00m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// An infinite loop![39m
|
||||
[38;5;252m [39m[38;5;246;03m// An infinite loop![39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// Do-while loops are like while loops, except they always run at least once.[39m
|
||||
[38;5;246;03m// Do-while loops are like while loops, except they always run at least once.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252minput[39m[38;5;252m;[39m
|
||||
[38;5;70;01mdo[39;00m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252minput[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mgetInput[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;70;01mwhile[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252m![39m[38;5;252misValid[39m[38;5;252m([39m[38;5;252minput[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// The `for` loop is the same as C and Java:[39m
|
||||
[38;5;246m// initialization; continue condition; iteration.[39m
|
||||
[38;5;246;03m// The `for` loop is the same as C and Java:[39;00m
|
||||
[38;5;246;03m// initialization; continue condition; iteration.[39;00m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m++[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// will run 5 times[39m
|
||||
[38;5;252m [39m[38;5;246;03m// will run 5 times[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// Breaking out of labeled loops is similar to Java[39m
|
||||
[38;5;246;03m// Breaking out of labeled loops is similar to Java[39;00m
|
||||
[38;5;252mouter[39m[38;5;252m:[39m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m++[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mj[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mj[39m[38;5;252m++[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m [39m[38;5;252m&&[39m[38;5;252m [39m[38;5;252mj[39m[38;5;252m [39m[38;5;252m==[39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mbreak[39;00m[38;5;252m [39m[38;5;252mouter[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// breaks out of outer loop instead of only the inner one[39m
|
||||
[38;5;252m [39m[38;5;246;03m// breaks out of outer loop instead of only the inner one[39;00m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// The for/in statement allows iteration over properties of an object.[39m
|
||||
[38;5;246;03m// The for/in statement allows iteration over properties of an object.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mdescription[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m""[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mperson[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252mfname[39m[38;5;252m:[39m[38;5;214m"Paul"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mlname[39m[38;5;252m:[39m[38;5;214m"Ken"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mage[39m[38;5;252m:[39m[38;5;67m18[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mperson[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mdescription[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;252mperson[39m[38;5;252m[[39m[38;5;252mx[39m[38;5;252m][39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m" "[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246m// description = 'Paul Ken 18 '[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246;03m// description = 'Paul Ken 18 '[39;00m
|
||||
|
||||
[38;5;246m// The for/of statement allows iteration over iterable objects (including the built-in String, [39m
|
||||
[38;5;246m// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, [39m
|
||||
[38;5;246m// and user-defined iterables).[39m
|
||||
[38;5;246;03m// The for/of statement allows iteration over iterable objects (including the built-in String, [39;00m
|
||||
[38;5;246;03m// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, [39;00m
|
||||
[38;5;246;03m// and user-defined iterables).[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyPets[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m""[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mpets[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"cat"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"dog"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"hamster"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"hedgehog"[39m[38;5;252m][39m[38;5;252m;[39m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mpet[39m[38;5;252m [39m[38;5;70;01mof[39;00m[38;5;252m [39m[38;5;252mpets[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyPets[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;252mpet[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m" "[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246m// myPets = 'cat dog hamster hedgehog '[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246;03m// myPets = 'cat dog hamster hedgehog '[39;00m
|
||||
|
||||
[38;5;246m// && is logical and, || is logical or[39m
|
||||
[38;5;246;03m// && is logical and, || is logical or[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mhouse[39m[38;5;252m.[39m[38;5;252msize[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"big"[39m[38;5;252m [39m[38;5;252m&&[39m[38;5;252m [39m[38;5;252mhouse[39m[38;5;252m.[39m[38;5;252mcolour[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"blue"[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mhouse[39m[38;5;252m.[39m[38;5;252mcontains[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"bear"[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mcolour[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"red"[39m[38;5;252m [39m[38;5;252m||[39m[38;5;252m [39m[38;5;252mcolour[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"blue"[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// colour is either red or blue[39m
|
||||
[38;5;252m [39m[38;5;246;03m// colour is either red or blue[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// && and || "short circuit", which is useful for setting default values.[39m
|
||||
[38;5;246;03m// && and || "short circuit", which is useful for setting default values.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252motherName[39m[38;5;252m [39m[38;5;252m||[39m[38;5;252m [39m[38;5;214m"default"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// The `switch` statement checks for equality with `===`.[39m
|
||||
[38;5;246m// Use 'break' after each case[39m
|
||||
[38;5;246m// or the cases after the correct one will be executed too.[39m
|
||||
[38;5;246;03m// The `switch` statement checks for equality with `===`.[39;00m
|
||||
[38;5;246;03m// Use 'break' after each case[39;00m
|
||||
[38;5;246;03m// or the cases after the correct one will be executed too.[39;00m
|
||||
[38;5;252mgrade[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'B'[39m[38;5;252m;[39m
|
||||
[38;5;70;01mswitch[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mgrade[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mcase[39;00m[38;5;252m [39m[38;5;214m'A'[39m[38;5;252m:[39m
|
||||
@ -282,172 +282,172 @@
|
||||
[38;5;252m}[39m
|
||||
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 4. Functions, Scope and Closures[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 4. Functions, Scope and Closures[39;00m
|
||||
|
||||
[38;5;246m// JavaScript functions are declared with the `function` keyword.[39m
|
||||
[38;5;246;03m// JavaScript functions are declared with the `function` keyword.[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252mthing[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mthing[39m[38;5;252m.[39m[38;5;252mtoUpperCase[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;214m"foo"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "FOO"[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;214m"foo"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "FOO"[39;00m
|
||||
|
||||
[38;5;246m// Note that the value to be returned must start on the same line as the[39m
|
||||
[38;5;246m// `return` keyword, otherwise you'll always return `undefined` due to[39m
|
||||
[38;5;246m// automatic semicolon insertion. Watch out for this when using Allman style.[39m
|
||||
[38;5;246;03m// Note that the value to be returned must start on the same line as the[39;00m
|
||||
[38;5;246;03m// `return` keyword, otherwise you'll always return `undefined` due to[39;00m
|
||||
[38;5;246;03m// automatic semicolon insertion. Watch out for this when using Allman style.[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;246m// <- semicolon automatically inserted here[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;246;03m// <- semicolon automatically inserted here[39;00m
|
||||
[38;5;252m [39m[38;5;252m{[39m[38;5;252mthisIsAn[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m'object literal'[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m// JavaScript functions are first class objects, so they can be reassigned to[39m
|
||||
[38;5;246m// different variable names and passed to other functions as arguments - for[39m
|
||||
[38;5;246m// example, when supplying an event handler:[39m
|
||||
[38;5;246;03m// JavaScript functions are first class objects, so they can be reassigned to[39;00m
|
||||
[38;5;246;03m// different variable names and passed to other functions as arguments - for[39;00m
|
||||
[38;5;246;03m// example, when supplying an event handler:[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// this code will be called in 5 seconds' time[39m
|
||||
[38;5;252m [39m[38;5;246;03m// this code will be called in 5 seconds' time[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252msetTimeout[39m[38;5;252m([39m[38;5;252mmyFunction[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;246m// Note: setTimeout isn't part of the JS language, but is provided by browsers[39m
|
||||
[38;5;246m// and Node.js.[39m
|
||||
[38;5;246;03m// Note: setTimeout isn't part of the JS language, but is provided by browsers[39;00m
|
||||
[38;5;246;03m// and Node.js.[39;00m
|
||||
|
||||
[38;5;246m// Another function provided by browsers is setInterval[39m
|
||||
[38;5;246;03m// Another function provided by browsers is setInterval[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// this code will be called every 5 seconds[39m
|
||||
[38;5;252m [39m[38;5;246;03m// this code will be called every 5 seconds[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252msetInterval[39m[38;5;252m([39m[38;5;252mmyFunction[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Function objects don't even have to be declared with a name - you can write[39m
|
||||
[38;5;246m// an anonymous function definition directly into the arguments of another.[39m
|
||||
[38;5;246;03m// Function objects don't even have to be declared with a name - you can write[39;00m
|
||||
[38;5;246;03m// an anonymous function definition directly into the arguments of another.[39;00m
|
||||
[38;5;252msetTimeout[39m[38;5;252m([39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// this code will be called in 5 seconds' time[39m
|
||||
[38;5;252m [39m[38;5;246;03m// this code will be called in 5 seconds' time[39;00m
|
||||
[38;5;252m}[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// JavaScript has function scope; functions get their own scope but other blocks[39m
|
||||
[38;5;246m// do not.[39m
|
||||
[38;5;246;03m// JavaScript has function scope; functions get their own scope but other blocks[39;00m
|
||||
[38;5;246;03m// do not.[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mtrue[39;00m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252mi[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5 - not undefined as you'd expect in a block-scoped language[39m
|
||||
[38;5;252mi[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5 - not undefined as you'd expect in a block-scoped language[39;00m
|
||||
|
||||
[38;5;246m// This has led to a common pattern of "immediately-executing anonymous[39m
|
||||
[38;5;246m// functions", which prevent temporary variables from leaking into the global[39m
|
||||
[38;5;246m// scope.[39m
|
||||
[38;5;246;03m// This has led to a common pattern of "immediately-executing anonymous[39;00m
|
||||
[38;5;246;03m// functions", which prevent temporary variables from leaking into the global[39;00m
|
||||
[38;5;246;03m// scope.[39;00m
|
||||
[38;5;252m([39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mtemporary[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// We can access the global scope by assigning to the "global object", which[39m
|
||||
[38;5;252m [39m[38;5;246m// in a web browser is always `window`. The global object may have a[39m
|
||||
[38;5;252m [39m[38;5;246m// different name in non-browser environments such as Node.js.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// We can access the global scope by assigning to the "global object", which[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// in a web browser is always `window`. The global object may have a[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// different name in non-browser environments such as Node.js.[39;00m
|
||||
[38;5;252m [39m[38;5;31mwindow[39m[38;5;252m.[39m[38;5;252mpermanent[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m)[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mtemporary[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// raises ReferenceError[39m
|
||||
[38;5;252mpermanent[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 10[39m
|
||||
[38;5;252mtemporary[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// raises ReferenceError[39;00m
|
||||
[38;5;252mpermanent[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 10[39;00m
|
||||
|
||||
[38;5;246m// One of JavaScript's most powerful features is closures. If a function is[39m
|
||||
[38;5;246m// defined inside another function, the inner function has access to all the[39m
|
||||
[38;5;246m// outer function's variables, even after the outer function exits.[39m
|
||||
[38;5;246;03m// One of JavaScript's most powerful features is closures. If a function is[39;00m
|
||||
[38;5;246;03m// defined inside another function, the inner function has access to all the[39;00m
|
||||
[38;5;246;03m// outer function's variables, even after the outer function exits.[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252msayHelloInFiveSeconds[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mprompt[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"Hello, "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"!"[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// Inner functions are put in the local scope by default, as if they were[39m
|
||||
[38;5;252m [39m[38;5;246m// declared with `var`.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// Inner functions are put in the local scope by default, as if they were[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// declared with `var`.[39;00m
|
||||
[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252minner[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252malert[39m[38;5;252m([39m[38;5;252mprompt[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m [39m[38;5;252msetTimeout[39m[38;5;252m([39m[38;5;252minner[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will[39m
|
||||
[38;5;252m [39m[38;5;246m// exit immediately, and setTimeout will call inner afterwards. However,[39m
|
||||
[38;5;252m [39m[38;5;246m// because inner is "closed over" sayHelloInFiveSeconds, inner still has[39m
|
||||
[38;5;252m [39m[38;5;246m// access to the `prompt` variable when it is finally called.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// exit immediately, and setTimeout will call inner afterwards. However,[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// because inner is "closed over" sayHelloInFiveSeconds, inner still has[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// access to the `prompt` variable when it is finally called.[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252msayHelloInFiveSeconds[39m[38;5;252m([39m[38;5;214m"Adam"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// will open a popup with "Hello, Adam!" in 5s[39m
|
||||
[38;5;252msayHelloInFiveSeconds[39m[38;5;252m([39m[38;5;214m"Adam"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// will open a popup with "Hello, Adam!" in 5s[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 5. More about Objects; Constructors and Prototypes[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 5. More about Objects; Constructors and Prototypes[39;00m
|
||||
|
||||
[38;5;246m// Objects can contain functions.[39m
|
||||
[38;5;246;03m// Objects can contain functions.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyFunc[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m"Hello world!"[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world!"[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world!"[39;00m
|
||||
|
||||
[38;5;246m// When functions attached to an object are called, they can access the object[39m
|
||||
[38;5;246m// they're attached to using the `this` keyword.[39m
|
||||
[38;5;246;03m// When functions attached to an object are called, they can access the object[39;00m
|
||||
[38;5;246;03m// they're attached to using the `this` keyword.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyString[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"Hello world!"[39m[38;5;252m,[39m
|
||||
[38;5;252m [39m[38;5;252mmyFunc[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyString[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world!"[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world!"[39;00m
|
||||
|
||||
[38;5;246m// What this is set to has to do with how the function is called, not where[39m
|
||||
[38;5;246m// it's defined. So, our function doesn't work if it isn't called in the[39m
|
||||
[38;5;246m// context of the object.[39m
|
||||
[38;5;246;03m// What this is set to has to do with how the function is called, not where[39;00m
|
||||
[38;5;246;03m// it's defined. So, our function doesn't work if it isn't called in the[39;00m
|
||||
[38;5;246;03m// context of the object.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m;[39m
|
||||
[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m// Inversely, a function can be assigned to the object and gain access to it[39m
|
||||
[38;5;246m// through `this`, even if it wasn't attached when it was defined.[39m
|
||||
[38;5;246;03m// Inversely, a function can be assigned to the object and gain access to it[39;00m
|
||||
[38;5;246;03m// through `this`, even if it wasn't attached when it was defined.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyOtherFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyString[39m[38;5;252m.[39m[38;5;252mtoUpperCase[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyOtherFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyOtherFunc[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyOtherFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "HELLO WORLD!"[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyOtherFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "HELLO WORLD!"[39;00m
|
||||
|
||||
[38;5;246m// We can also specify a context for a function to execute in when we invoke it[39m
|
||||
[38;5;246m// using `call` or `apply`.[39m
|
||||
[38;5;246;03m// We can also specify a context for a function to execute in when we invoke it[39;00m
|
||||
[38;5;246;03m// using `call` or `apply`.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252manotherFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252ms[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyString[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mcall[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m" And Hello Moon!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello World! And Hello Moon!"[39m
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mcall[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m" And Hello Moon!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello World! And Hello Moon!"[39;00m
|
||||
|
||||
[38;5;246m// The `apply` function is nearly identical, but takes an array for an argument[39m
|
||||
[38;5;246m// list.[39m
|
||||
[38;5;246;03m// The `apply` function is nearly identical, but takes an array for an argument[39;00m
|
||||
[38;5;246;03m// list.[39;00m
|
||||
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m" And Hello Sun!"[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello World! And Hello Sun!"[39m
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m" And Hello Sun!"[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello World! And Hello Sun!"[39;00m
|
||||
|
||||
[38;5;246m// This is useful when working with a function that accepts a sequence of[39m
|
||||
[38;5;246m// arguments and you want to pass an array.[39m
|
||||
[38;5;246;03m// This is useful when working with a function that accepts a sequence of[39;00m
|
||||
[38;5;246;03m// arguments and you want to pass an array.[39;00m
|
||||
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 6[39m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = NaN (uh-oh!)[39m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;31mMath[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 6[39m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 6[39;00m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = NaN (uh-oh!)[39;00m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;31mMath[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 6[39;00m
|
||||
|
||||
[38;5;246m// But, `call` and `apply` are only temporary. When we want it to stick, we can[39m
|
||||
[38;5;246m// use `bind`.[39m
|
||||
[38;5;246;03m// But, `call` and `apply` are only temporary. When we want it to stick, we can[39;00m
|
||||
[38;5;246;03m// use `bind`.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mboundFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mbind[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mboundFunc[39m[38;5;252m([39m[38;5;214m" And Hello Saturn!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello World! And Hello Saturn!"[39m
|
||||
[38;5;252mboundFunc[39m[38;5;252m([39m[38;5;214m" And Hello Saturn!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello World! And Hello Saturn!"[39;00m
|
||||
|
||||
[38;5;246m// `bind` can also be used to partially apply (curry) a function.[39m
|
||||
[38;5;246;03m// `bind` can also be used to partially apply (curry) a function.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mproduct[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252ma[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mb[39m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252ma[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;252mb[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mdoubler[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mproduct[39m[38;5;252m.[39m[38;5;252mbind[39m[38;5;252m([39m[38;5;70;01mthis[39;00m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mdoubler[39m[38;5;252m([39m[38;5;67m8[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 16[39m
|
||||
[38;5;252mdoubler[39m[38;5;252m([39m[38;5;67m8[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 16[39;00m
|
||||
|
||||
[38;5;246m// When you call a function with the `new` keyword, a new object is created, and[39m
|
||||
[38;5;246m// made available to the function via the `this` keyword. Functions designed to be[39m
|
||||
[38;5;246m// called like that are called constructors.[39m
|
||||
[38;5;246;03m// When you call a function with the `new` keyword, a new object is created, and[39;00m
|
||||
[38;5;246;03m// made available to the function via the `this` keyword. Functions designed to be[39;00m
|
||||
[38;5;246;03m// called like that are called constructors.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = {myNumber: 5}[39m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5[39m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = {myNumber: 5}[39;00m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5[39;00m
|
||||
|
||||
[38;5;246m// Unlike most other popular object-oriented languages, JavaScript has no[39m
|
||||
[38;5;246m// concept of 'instances' created from 'class' blueprints; instead, JavaScript[39m
|
||||
[38;5;246m// combines instantiation and inheritance into a single concept: a 'prototype'.[39m
|
||||
[38;5;246;03m// Unlike most other popular object-oriented languages, JavaScript has no[39;00m
|
||||
[38;5;246;03m// concept of 'instances' created from 'class' blueprints; instead, JavaScript[39;00m
|
||||
[38;5;246;03m// combines instantiation and inheritance into a single concept: a 'prototype'.[39;00m
|
||||
|
||||
[38;5;246m// Every JavaScript object has a 'prototype'. When you go to access a property[39m
|
||||
[38;5;246m// on an object that doesn't exist on the actual object, the interpreter will[39m
|
||||
[38;5;246m// look at its prototype.[39m
|
||||
[38;5;246;03m// Every JavaScript object has a 'prototype'. When you go to access a property[39;00m
|
||||
[38;5;246;03m// on an object that doesn't exist on the actual object, the interpreter will[39;00m
|
||||
[38;5;246;03m// look at its prototype.[39;00m
|
||||
|
||||
[38;5;246m// Some JS implementations let you access an object's prototype on the magic[39m
|
||||
[38;5;246m// property `__proto__`. While this is useful for explaining prototypes it's not[39m
|
||||
[38;5;246m// part of the standard; we'll get to standard ways of using prototypes later.[39m
|
||||
[38;5;246;03m// Some JS implementations let you access an object's prototype on the magic[39;00m
|
||||
[38;5;246;03m// property `__proto__`. While this is useful for explaining prototypes it's not[39;00m
|
||||
[38;5;246;03m// part of the standard; we'll get to standard ways of using prototypes later.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyString[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"Hello world!"[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
@ -459,58 +459,58 @@
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252m__proto__[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyPrototype[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 42[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 42[39;00m
|
||||
|
||||
[38;5;246m// This works for functions, too.[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "hello world!"[39m
|
||||
[38;5;246;03m// This works for functions, too.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "hello world!"[39;00m
|
||||
|
||||
[38;5;246m// Of course, if your property isn't on your prototype, the prototype's[39m
|
||||
[38;5;246m// prototype is searched, and so on.[39m
|
||||
[38;5;246;03m// Of course, if your property isn't on your prototype, the prototype's[39;00m
|
||||
[38;5;246;03m// prototype is searched, and so on.[39;00m
|
||||
[38;5;252mmyPrototype[39m[38;5;252m.[39m[38;5;252m__proto__[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyBoolean[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mtrue[39;00m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyBoolean[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyBoolean[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// There's no copying involved here; each object stores a reference to its[39m
|
||||
[38;5;246m// prototype. This means we can alter the prototype and our changes will be[39m
|
||||
[38;5;246m// reflected everywhere.[39m
|
||||
[38;5;246;03m// There's no copying involved here; each object stores a reference to its[39;00m
|
||||
[38;5;246;03m// prototype. This means we can alter the prototype and our changes will be[39;00m
|
||||
[38;5;246;03m// reflected everywhere.[39;00m
|
||||
[38;5;252mmyPrototype[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m43[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 43[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 43[39;00m
|
||||
|
||||
[38;5;246m// The for/in statement allows iteration over properties of an object,[39m
|
||||
[38;5;246m// walking up the prototype chain until it sees a null prototype.[39m
|
||||
[38;5;246;03m// The for/in statement allows iteration over properties of an object,[39;00m
|
||||
[38;5;246;03m// walking up the prototype chain until it sees a null prototype.[39;00m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mconsole[39m[38;5;252m.[39m[38;5;252mlog[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;252mx[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;246m///prints:[39m
|
||||
[38;5;246m// Hello world![39m
|
||||
[38;5;246m// 43[39m
|
||||
[38;5;246m// [Function: myFunc][39m
|
||||
[38;5;246m// true[39m
|
||||
[38;5;246;03m///prints:[39;00m
|
||||
[38;5;246;03m// Hello world![39;00m
|
||||
[38;5;246;03m// 43[39;00m
|
||||
[38;5;246;03m// [Function: myFunc][39;00m
|
||||
[38;5;246;03m// true[39;00m
|
||||
|
||||
[38;5;246m// To only consider properties attached to the object itself[39m
|
||||
[38;5;246m// and not its prototypes, use the `hasOwnProperty()` check.[39m
|
||||
[38;5;246;03m// To only consider properties attached to the object itself[39;00m
|
||||
[38;5;246;03m// and not its prototypes, use the `hasOwnProperty()` check.[39;00m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mhasOwnProperty[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mconsole[39m[38;5;252m.[39m[38;5;252mlog[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;252mx[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;246m///prints:[39m
|
||||
[38;5;246m// Hello world![39m
|
||||
[38;5;246;03m///prints:[39;00m
|
||||
[38;5;246;03m// Hello world![39;00m
|
||||
|
||||
[38;5;246m// We mentioned that `__proto__` was non-standard, and there's no standard way to[39m
|
||||
[38;5;246m// change the prototype of an existing object. However, there are two ways to[39m
|
||||
[38;5;246m// create a new object with a given prototype.[39m
|
||||
[38;5;246;03m// We mentioned that `__proto__` was non-standard, and there's no standard way to[39;00m
|
||||
[38;5;246;03m// change the prototype of an existing object. However, there are two ways to[39;00m
|
||||
[38;5;246;03m// create a new object with a given prototype.[39;00m
|
||||
|
||||
[38;5;246m// The first is Object.create, which is a recent addition to JS, and therefore[39m
|
||||
[38;5;246m// not available in all implementations yet.[39m
|
||||
[38;5;246;03m// The first is Object.create, which is a recent addition to JS, and therefore[39;00m
|
||||
[38;5;246;03m// not available in all implementations yet.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m([39m[38;5;252mmyPrototype[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 43[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 43[39;00m
|
||||
|
||||
[38;5;246m// The second way, which works anywhere, has to do with constructors.[39m
|
||||
[38;5;246m// Constructors have a property called prototype. This is *not* the prototype of[39m
|
||||
[38;5;246m// the constructor function itself; instead, it's the prototype that new objects[39m
|
||||
[38;5;246m// are given when they're created with that constructor and the new keyword.[39m
|
||||
[38;5;246;03m// The second way, which works anywhere, has to do with constructors.[39;00m
|
||||
[38;5;246;03m// Constructors have a property called prototype. This is *not* the prototype of[39;00m
|
||||
[38;5;246;03m// the constructor function itself; instead, it's the prototype that new objects[39;00m
|
||||
[38;5;246;03m// are given when they're created with that constructor and the new keyword.[39;00m
|
||||
[38;5;252mMyConstructor[39m[38;5;252m.[39m[38;5;252mprototype[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m
|
||||
[38;5;252m [39m[38;5;252mgetMyNumber[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
@ -518,47 +518,89 @@
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyNewObj2[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5[39;00m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 6[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 6[39;00m
|
||||
|
||||
[38;5;246m// Built-in types like strings and numbers also have constructors that create[39m
|
||||
[38;5;246m// equivalent wrapper objects.[39m
|
||||
[38;5;246;03m// Built-in types like strings and numbers also have constructors that create[39;00m
|
||||
[38;5;246;03m// equivalent wrapper objects.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m12[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;31mNumber[39m[38;5;252m([39m[38;5;67m12[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Except, they aren't exactly equivalent.[39m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 'number'[39m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 'object'[39m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;246;03m// Except, they aren't exactly equivalent.[39;00m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 'number'[39;00m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 'object'[39;00m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// This code won't execute, because 0 is falsy.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// This code won't execute, because 0 is falsy.[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;31mNumber[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// This code will execute, because wrapped numbers are objects, and objects[39m
|
||||
[38;5;252m [39m[38;5;246m// are always truthy.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// This code will execute, because wrapped numbers are objects, and objects[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// are always truthy.[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// However, the wrapper objects and the regular builtins share a prototype, so[39m
|
||||
[38;5;246m// you can actually add functionality to a string, for instance.[39m
|
||||
[38;5;246;03m// However, the wrapper objects and the regular builtins share a prototype, so[39;00m
|
||||
[38;5;246;03m// you can actually add functionality to a string, for instance.[39;00m
|
||||
[38;5;31mString[39m[38;5;252m.[39m[38;5;252mprototype[39m[38;5;252m.[39m[38;5;252mfirstCharacter[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mcharAt[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;214m"abc"[39m[38;5;252m.[39m[38;5;252mfirstCharacter[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "a"[39m
|
||||
[38;5;214m"abc"[39m[38;5;252m.[39m[38;5;252mfirstCharacter[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "a"[39;00m
|
||||
|
||||
[38;5;246m// This fact is often used in "polyfilling", which is implementing newer[39m
|
||||
[38;5;246m// features of JavaScript in an older subset of JavaScript, so that they can be[39m
|
||||
[38;5;246m// used in older environments such as outdated browsers.[39m
|
||||
[38;5;246;03m// This fact is often used in "polyfilling", which is implementing newer[39;00m
|
||||
[38;5;246;03m// features of JavaScript in an older subset of JavaScript, so that they can be[39;00m
|
||||
[38;5;246;03m// used in older environments such as outdated browsers.[39;00m
|
||||
|
||||
[38;5;246m// For instance, we mentioned that Object.create isn't yet available in all[39m
|
||||
[38;5;246m// implementations, but we can still use it with this polyfill:[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m [39m[38;5;246m// don't overwrite it if it exists[39m
|
||||
[38;5;246;03m// For instance, we mentioned that Object.create isn't yet available in all[39;00m
|
||||
[38;5;246;03m// implementations, but we can still use it with this polyfill:[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m [39m[38;5;246;03m// don't overwrite it if it exists[39;00m
|
||||
[38;5;252m [39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252mproto[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// make a temporary constructor with the right prototype[39m
|
||||
[38;5;252m [39m[38;5;246;03m// make a temporary constructor with the right prototype[39;00m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mConstructor[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252mConstructor[39m[38;5;252m.[39m[38;5;252mprototype[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mproto[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// then use it to create a new, appropriately-prototyped object[39m
|
||||
[38;5;252m [39m[38;5;246;03m// then use it to create a new, appropriately-prototyped object[39;00m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246;03m// ES6 Additions[39;00m
|
||||
|
||||
[38;5;246;03m// The "let" keyword allows you to define variables in a lexical scope, [39;00m
|
||||
[38;5;246;03m// as opposed to a block scope like the var keyword does.[39;00m
|
||||
[38;5;70;01mlet[39;00m[38;5;252m [39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"Billy"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246;03m// Variables defined with let can be reassigned new values.[39;00m
|
||||
[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"William"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246;03m// The "const" keyword allows you to define a variable in a lexical scope[39;00m
|
||||
[38;5;246;03m// like with let, but you cannot reassign the value once one has been assigned.[39;00m
|
||||
|
||||
[38;5;70;01mconst[39;00m[38;5;252m [39m[38;5;252mpi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m3.14[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;252mpi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m4.13[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// You cannot do this.[39;00m
|
||||
|
||||
[38;5;246;03m// There is a new syntax for functions in ES6 known as "lambda syntax".[39;00m
|
||||
[38;5;246;03m// This allows functions to be defined in a lexical scope like with variables[39;00m
|
||||
[38;5;246;03m// defined by const and let. [39;00m
|
||||
|
||||
[38;5;70;01mconst[39;00m[38;5;252m [39m[38;5;252misEven[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;252mnumber[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m=>[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mnumber[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;252misEven[39m[38;5;252m([39m[38;5;67m7[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// false[39;00m
|
||||
|
||||
[38;5;246;03m// The "equivalent" of this function in the traditional syntax would look like this:[39;00m
|
||||
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252misEven[39m[38;5;252m([39m[38;5;252mnumber[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mnumber[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246;03m// I put the word "equivalent" in double quotes because a function defined[39;00m
|
||||
[38;5;246;03m// using the lambda syntax cannnot be called before the definition.[39;00m
|
||||
[38;5;246;03m// The following is an example of invalid usage:[39;00m
|
||||
|
||||
[38;5;252madd[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m8[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;70;01mconst[39;00m[38;5;252m [39m[38;5;252madd[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;252mfirstNumber[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252msecondNumber[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m=>[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mfirstNumber[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252msecondNumber[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
630
tests/results/22
630
tests/results/22
@ -1,270 +1,270 @@
|
||||
[38;5;246m// Single-line comments start with two slashes.[39m
|
||||
[38;5;246m/* Multiline comments start with slash-star,[39m
|
||||
[38;5;246m and end with star-slash */[39m
|
||||
[38;5;246;03m// Single-line comments start with two slashes.[39;00m
|
||||
[38;5;246;03m/* Multiline comments start with slash-star,[39;00m
|
||||
[38;5;246;03m and end with star-slash */[39;00m
|
||||
|
||||
[38;5;246m// Statements can be terminated by ;[39m
|
||||
[38;5;246;03m// Statements can be terminated by ;[39;00m
|
||||
[38;5;252mdoStuff[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// ... but they don't have to be, as semicolons are automatically inserted[39m
|
||||
[38;5;246m// wherever there's a newline, except in certain cases.[39m
|
||||
[38;5;246;03m// ... but they don't have to be, as semicolons are automatically inserted[39;00m
|
||||
[38;5;246;03m// wherever there's a newline, except in certain cases.[39;00m
|
||||
[38;5;252mdoStuff[39m[38;5;252m([39m[38;5;252m)[39m
|
||||
|
||||
[38;5;246m// Because those cases can cause unexpected results, we'll keep on using[39m
|
||||
[38;5;246m// semicolons in this guide.[39m
|
||||
[38;5;246;03m// Because those cases can cause unexpected results, we'll keep on using[39;00m
|
||||
[38;5;246;03m// semicolons in this guide.[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 1. Numbers, Strings and Operators[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 1. Numbers, Strings and Operators[39;00m
|
||||
|
||||
[38;5;246m// JavaScript has one number type (which is a 64-bit IEEE 754 double).[39m
|
||||
[38;5;246m// Doubles have a 52-bit mantissa, which is enough to store integers[39m
|
||||
[38;5;246m// up to about 9✕10¹⁵ precisely.[39m
|
||||
[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 3[39m
|
||||
[38;5;67m1.5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 1.5[39m
|
||||
[38;5;246;03m// JavaScript has one number type (which is a 64-bit IEEE 754 double).[39;00m
|
||||
[38;5;246;03m// Doubles have a 52-bit mantissa, which is enough to store integers[39;00m
|
||||
[38;5;246;03m// up to about 9✕10¹⁵ precisely.[39;00m
|
||||
[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 3[39;00m
|
||||
[38;5;67m1.5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 1.5[39;00m
|
||||
|
||||
[38;5;246m// Some basic arithmetic works as you'd expect.[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 2[39m
|
||||
[38;5;67m0.1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m0.2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 0.30000000000000004[39m
|
||||
[38;5;67m8[39m[38;5;252m [39m[38;5;252m-[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 7[39m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 20[39m
|
||||
[38;5;67m35[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 7[39m
|
||||
[38;5;246;03m// Some basic arithmetic works as you'd expect.[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 2[39;00m
|
||||
[38;5;67m0.1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m0.2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 0.30000000000000004[39;00m
|
||||
[38;5;67m8[39m[38;5;252m [39m[38;5;252m-[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 7[39;00m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 20[39;00m
|
||||
[38;5;67m35[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 7[39;00m
|
||||
|
||||
[38;5;246m// Including uneven division.[39m
|
||||
[38;5;67m5[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 2.5[39m
|
||||
[38;5;246;03m// Including uneven division.[39;00m
|
||||
[38;5;67m5[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 2.5[39;00m
|
||||
|
||||
[38;5;246m// And modulo division.[39m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 0[39m
|
||||
[38;5;67m30[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 2[39m
|
||||
[38;5;67m18.5[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m7[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4.5[39m
|
||||
[38;5;246;03m// And modulo division.[39;00m
|
||||
[38;5;67m10[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 0[39;00m
|
||||
[38;5;67m30[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 2[39;00m
|
||||
[38;5;67m18.5[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m7[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4.5[39;00m
|
||||
|
||||
[38;5;246m// Bitwise operations also work; when you perform a bitwise operation your float[39m
|
||||
[38;5;246m// is converted to a signed int *up to* 32 bits.[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4[39m
|
||||
[38;5;246;03m// Bitwise operations also work; when you perform a bitwise operation your float[39;00m
|
||||
[38;5;246;03m// is converted to a signed int *up to* 32 bits.[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<<[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4[39;00m
|
||||
|
||||
[38;5;246m// Precedence is enforced with parentheses.[39m
|
||||
[38;5;252m([39m[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 8[39m
|
||||
[38;5;246;03m// Precedence is enforced with parentheses.[39;00m
|
||||
[38;5;252m([39m[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 8[39;00m
|
||||
|
||||
[38;5;246m// There are three special not-a-real-number values:[39m
|
||||
[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// result of e.g. 1/0[39m
|
||||
[38;5;252m-[39m[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// result of e.g. -1/0[39m
|
||||
[38;5;70;01mNaN[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// result of e.g. 0/0, stands for 'Not a Number'[39m
|
||||
[38;5;246;03m// There are three special not-a-real-number values:[39;00m
|
||||
[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// result of e.g. 1/0[39;00m
|
||||
[38;5;252m-[39m[38;5;70;01mInfinity[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// result of e.g. -1/0[39;00m
|
||||
[38;5;70;01mNaN[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// result of e.g. 0/0, stands for 'Not a Number'[39;00m
|
||||
|
||||
[38;5;246m// There's also a boolean type.[39m
|
||||
[38;5;246;03m// There's also a boolean type.[39;00m
|
||||
[38;5;70;01mtrue[39;00m[38;5;252m;[39m
|
||||
[38;5;70;01mfalse[39;00m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Strings are created with ' or ".[39m
|
||||
[38;5;246;03m// Strings are created with ' or ".[39;00m
|
||||
[38;5;214m'abc'[39m[38;5;252m;[39m
|
||||
[38;5;214m"Hello, world"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Negation uses the ! symbol[39m
|
||||
[38;5;252m![39m[38;5;70;01mtrue[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;252m![39m[38;5;70;01mfalse[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// Negation uses the ! symbol[39;00m
|
||||
[38;5;252m![39m[38;5;70;01mtrue[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;252m![39m[38;5;70;01mfalse[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Equality is ===[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;246;03m// Equality is ===[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
|
||||
[38;5;246m// Inequality is !==[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// Inequality is !==[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m!==[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// More comparisons[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m<=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m>=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// More comparisons[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;67m1[39m[38;5;252m [39m[38;5;252m>[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m<=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;67m2[39m[38;5;252m [39m[38;5;252m>=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Strings are concatenated with +[39m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"world!"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world!"[39m
|
||||
[38;5;246;03m// Strings are concatenated with +[39;00m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"world!"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world!"[39;00m
|
||||
|
||||
[38;5;246m// ... which works with more than just strings[39m
|
||||
[38;5;214m"1, 2, "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "1, 2, 3"[39m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"world"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"!"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world,!"[39m
|
||||
[38;5;246;03m// ... which works with more than just strings[39;00m
|
||||
[38;5;214m"1, 2, "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "1, 2, 3"[39;00m
|
||||
[38;5;214m"Hello "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"world"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"!"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world,!"[39;00m
|
||||
|
||||
[38;5;246m// and are compared with < and >[39m
|
||||
[38;5;214m"a"[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;214m"b"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// and are compared with < and >[39;00m
|
||||
[38;5;214m"a"[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;214m"b"[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Type coercion is performed for comparisons with double equals...[39m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;246;03m// Type coercion is performed for comparisons with double equals...[39;00m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// ...unless you use ===[39m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;246;03m// ...unless you use ===[39;00m
|
||||
[38;5;214m"5"[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
|
||||
[38;5;246m// ...which can result in some weird behaviour...[39m
|
||||
[38;5;67m13[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// 14[39m
|
||||
[38;5;214m"13"[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// '13true'[39m
|
||||
[38;5;246;03m// ...which can result in some weird behaviour...[39;00m
|
||||
[38;5;67m13[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// 14[39;00m
|
||||
[38;5;214m"13"[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252m![39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// '13true'[39;00m
|
||||
|
||||
[38;5;246m// You can access characters in a string with `charAt`[39m
|
||||
[38;5;214m"This is a string"[39m[38;5;252m.[39m[38;5;252mcharAt[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 'T'[39m
|
||||
[38;5;246;03m// You can access characters in a string with `charAt`[39;00m
|
||||
[38;5;214m"This is a string"[39m[38;5;252m.[39m[38;5;252mcharAt[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 'T'[39;00m
|
||||
|
||||
[38;5;246m// ...or use `substring` to get larger pieces.[39m
|
||||
[38;5;214m"Hello world"[39m[38;5;252m.[39m[38;5;252msubstring[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello"[39m
|
||||
[38;5;246;03m// ...or use `substring` to get larger pieces.[39;00m
|
||||
[38;5;214m"Hello world"[39m[38;5;252m.[39m[38;5;252msubstring[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello"[39;00m
|
||||
|
||||
[38;5;246m// `length` is a property, so don't use ().[39m
|
||||
[38;5;214m"Hello"[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5[39m
|
||||
[38;5;246;03m// `length` is a property, so don't use ().[39;00m
|
||||
[38;5;214m"Hello"[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5[39;00m
|
||||
|
||||
[38;5;246m// There's also `null` and `undefined`.[39m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// used to indicate a deliberate non-value[39m
|
||||
[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// used to indicate a value is not currently present (although[39m
|
||||
[38;5;252m [39m[38;5;246m// `undefined` is actually a value itself)[39m
|
||||
[38;5;246;03m// There's also `null` and `undefined`.[39;00m
|
||||
[38;5;70;01mnull[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// used to indicate a deliberate non-value[39;00m
|
||||
[38;5;70;01mundefined[39;00m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// used to indicate a value is not currently present (although[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// `undefined` is actually a value itself)[39;00m
|
||||
|
||||
[38;5;246m// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.[39m
|
||||
[38;5;246m// Note that 0 is falsy and "0" is truthy, even though 0 == "0".[39m
|
||||
[38;5;246;03m// false, null, undefined, NaN, 0 and "" are falsy; everything else is truthy.[39;00m
|
||||
[38;5;246;03m// Note that 0 is falsy and "0" is truthy, even though 0 == "0".[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 2. Variables, Arrays and Objects[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 2. Variables, Arrays and Objects[39;00m
|
||||
|
||||
[38;5;246m// Variables are declared with the `var` keyword. JavaScript is dynamically[39m
|
||||
[38;5;246m// typed, so you don't need to specify type. Assignment uses a single `=`[39m
|
||||
[38;5;246m// character.[39m
|
||||
[38;5;246;03m// Variables are declared with the `var` keyword. JavaScript is dynamically[39;00m
|
||||
[38;5;246;03m// typed, so you don't need to specify type. Assignment uses a single `=`[39;00m
|
||||
[38;5;246;03m// character.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// If you leave the var keyword off, you won't get an error...[39m
|
||||
[38;5;246;03m// If you leave the var keyword off, you won't get an error...[39;00m
|
||||
[38;5;252msomeOtherVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// ...but your variable will be created in the global scope, not in the scope[39m
|
||||
[38;5;246m// you defined it in.[39m
|
||||
[38;5;246;03m// ...but your variable will be created in the global scope, not in the scope[39;00m
|
||||
[38;5;246;03m// you defined it in.[39;00m
|
||||
|
||||
[38;5;246m// Variables declared without being assigned to are set to undefined.[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeThirdVar[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;246;03m// Variables declared without being assigned to are set to undefined.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeThirdVar[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m// If you want to declare a couple of variables, then you could use a comma[39m
|
||||
[38;5;246m// separator[39m
|
||||
[38;5;246;03m// If you want to declare a couple of variables, then you could use a comma[39;00m
|
||||
[38;5;246;03m// separator[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252msomeFourthVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252msomeFifthVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// There's shorthand for performing math operations on variables:[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// equivalent to someVar = someVar + 5; someVar is 10 now[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m*=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// now someVar is 100[39m
|
||||
[38;5;246;03m// There's shorthand for performing math operations on variables:[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// equivalent to someVar = someVar + 5; someVar is 10 now[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m*=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// now someVar is 100[39;00m
|
||||
|
||||
[38;5;246m// and an even-shorter-hand for adding or subtracting 1[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m++[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// now someVar is 101[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m--[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// back to 100[39m
|
||||
[38;5;246;03m// and an even-shorter-hand for adding or subtracting 1[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m++[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// now someVar is 101[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m--[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// back to 100[39;00m
|
||||
|
||||
[38;5;246m// Arrays are ordered lists of values, of any type.[39m
|
||||
[38;5;246;03m// Arrays are ordered lists of values, of any type.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"Hello"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m45[39m[38;5;252m,[39m[38;5;252m [39m[38;5;70;01mtrue[39;00m[38;5;252m][39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Their members can be accessed using the square-brackets subscript syntax.[39m
|
||||
[38;5;246m// Array indices start at zero.[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 45[39m
|
||||
[38;5;246;03m// Their members can be accessed using the square-brackets subscript syntax.[39;00m
|
||||
[38;5;246;03m// Array indices start at zero.[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m[[39m[38;5;67m1[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 45[39;00m
|
||||
|
||||
[38;5;246m// Arrays are mutable and of variable length.[39m
|
||||
[38;5;246;03m// Arrays are mutable and of variable length.[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpush[39m[38;5;252m([39m[38;5;214m"World"[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mlength[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4[39;00m
|
||||
|
||||
[38;5;246m// Add/Modify at specific index[39m
|
||||
[38;5;246;03m// Add/Modify at specific index[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m[[39m[38;5;67m3[39m[38;5;252m][39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"Hello"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Add and remove element from front or back end of an array[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252munshift[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Add as the first element[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mshift[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Remove first element and return it[39m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpush[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Add as the last element[39m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpop[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// Remove last element and return it[39m
|
||||
[38;5;246;03m// Add and remove element from front or back end of an array[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252munshift[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Add as the first element[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mshift[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Remove first element and return it[39;00m
|
||||
[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpush[39m[38;5;252m([39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Add as the last element[39;00m
|
||||
[38;5;252msomeVar[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyArray[39m[38;5;252m.[39m[38;5;252mpop[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// Remove last element and return it[39;00m
|
||||
|
||||
[38;5;246m// Join all elements of an array with semicolon[39m
|
||||
[38;5;246;03m// Join all elements of an array with semicolon[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyArray0[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m32[39m[38;5;252m,[39m[38;5;70;01mfalse[39;00m[38;5;252m,[39m[38;5;214m"js"[39m[38;5;252m,[39m[38;5;67m12[39m[38;5;252m,[39m[38;5;67m56[39m[38;5;252m,[39m[38;5;67m90[39m[38;5;252m][39m[38;5;252m;[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mjoin[39m[38;5;252m([39m[38;5;214m";"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246m// = "32;false;js;12;56;90"[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mjoin[39m[38;5;252m([39m[38;5;214m";"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "32;false;js;12;56;90"[39;00m
|
||||
|
||||
[38;5;246m// Get subarray of elements from index 1 (include) to 4 (exclude)[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mslice[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = [false,"js",12][39m
|
||||
[38;5;246;03m// Get subarray of elements from index 1 (include) to 4 (exclude)[39;00m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252mslice[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = [false,"js",12][39;00m
|
||||
|
||||
[38;5;246m// Remove 4 elements starting from index 2, and insert there strings[39m
|
||||
[38;5;246m// "hi","wr" and "ld"; return removed subarray[39m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252msplice[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;214m"hi"[39m[38;5;252m,[39m[38;5;214m"wr"[39m[38;5;252m,[39m[38;5;214m"ld"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = ["js",12,56,90][39m
|
||||
[38;5;246m// myArray0 === [32,false,"hi","wr","ld"][39m
|
||||
[38;5;246;03m// Remove 4 elements starting from index 2, and insert there strings[39;00m
|
||||
[38;5;246;03m// "hi","wr" and "ld"; return removed subarray[39;00m
|
||||
[38;5;252mmyArray0[39m[38;5;252m.[39m[38;5;252msplice[39m[38;5;252m([39m[38;5;67m2[39m[38;5;252m,[39m[38;5;67m4[39m[38;5;252m,[39m[38;5;214m"hi"[39m[38;5;252m,[39m[38;5;214m"wr"[39m[38;5;252m,[39m[38;5;214m"ld"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = ["js",12,56,90][39;00m
|
||||
[38;5;246;03m// myArray0 === [32,false,"hi","wr","ld"][39;00m
|
||||
|
||||
[38;5;246m// JavaScript's objects are equivalent to "dictionaries" or "maps" in other[39m
|
||||
[38;5;246m// languages: an unordered collection of key-value pairs.[39m
|
||||
[38;5;246;03m// JavaScript's objects are equivalent to "dictionaries" or "maps" in other[39;00m
|
||||
[38;5;246;03m// languages: an unordered collection of key-value pairs.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252mkey1[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"Hello"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mkey2[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"World"[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Keys are strings, but quotes aren't required if they're a valid[39m
|
||||
[38;5;246m// JavaScript identifier. Values can be any type.[39m
|
||||
[38;5;246;03m// Keys are strings, but quotes aren't required if they're a valid[39;00m
|
||||
[38;5;246;03m// JavaScript identifier. Values can be any type.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252mmyKey[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"myValue"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"my other key"[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Object attributes can also be accessed using the subscript syntax,[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;214m"my other key"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 4[39m
|
||||
[38;5;246;03m// Object attributes can also be accessed using the subscript syntax,[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;214m"my other key"[39m[38;5;252m][39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 4[39;00m
|
||||
|
||||
[38;5;246m// ... or using the dot syntax, provided the key is a valid identifier.[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "myValue"[39m
|
||||
[38;5;246;03m// ... or using the dot syntax, provided the key is a valid identifier.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "myValue"[39;00m
|
||||
|
||||
[38;5;246m// Objects are mutable; values can be changed and new keys added.[39m
|
||||
[38;5;246;03m// Objects are mutable; values can be changed and new keys added.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyThirdKey[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mtrue[39;00m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// If you try to access a value that's not yet set, you'll get undefined.[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFourthKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;246;03m// If you try to access a value that's not yet set, you'll get undefined.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFourthKey[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 3. Logic and Control Structures[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 3. Logic and Control Structures[39;00m
|
||||
|
||||
[38;5;246m// The `if` structure works as you'd expect.[39m
|
||||
[38;5;246;03m// The `if` structure works as you'd expect.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mcount[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m1[39m[38;5;252m;[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mcount[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// evaluated if count is 3[39m
|
||||
[38;5;252m [39m[38;5;246;03m// evaluated if count is 3[39;00m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;70;01melse[39;00m[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mcount[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m4[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// evaluated if count is 4[39m
|
||||
[38;5;252m [39m[38;5;246;03m// evaluated if count is 4[39;00m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;70;01melse[39;00m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// evaluated if it's not either 3 or 4[39m
|
||||
[38;5;252m [39m[38;5;246;03m// evaluated if it's not either 3 or 4[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// As does `while`.[39m
|
||||
[38;5;246;03m// As does `while`.[39;00m
|
||||
[38;5;70;01mwhile[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mtrue[39;00m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// An infinite loop![39m
|
||||
[38;5;252m [39m[38;5;246;03m// An infinite loop![39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// Do-while loops are like while loops, except they always run at least once.[39m
|
||||
[38;5;246;03m// Do-while loops are like while loops, except they always run at least once.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252minput[39m[38;5;252m;[39m
|
||||
[38;5;70;01mdo[39;00m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252minput[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mgetInput[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;70;01mwhile[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252m![39m[38;5;252misValid[39m[38;5;252m([39m[38;5;252minput[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// The `for` loop is the same as C and Java:[39m
|
||||
[38;5;246m// initialization; continue condition; iteration.[39m
|
||||
[38;5;246;03m// The `for` loop is the same as C and Java:[39;00m
|
||||
[38;5;246;03m// initialization; continue condition; iteration.[39;00m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m++[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// will run 5 times[39m
|
||||
[38;5;252m [39m[38;5;246;03m// will run 5 times[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// Breaking out of labeled loops is similar to Java[39m
|
||||
[38;5;246;03m// Breaking out of labeled loops is similar to Java[39;00m
|
||||
[38;5;252mouter[39m[38;5;252m:[39m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mi[39m[38;5;252m++[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mj[39m[38;5;252m [39m[38;5;252m<[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252mj[39m[38;5;252m++[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m [39m[38;5;252m&&[39m[38;5;252m [39m[38;5;252mj[39m[38;5;252m [39m[38;5;252m==[39m[38;5;67m5[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mbreak[39;00m[38;5;252m [39m[38;5;252mouter[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// breaks out of outer loop instead of only the inner one[39m
|
||||
[38;5;252m [39m[38;5;246;03m// breaks out of outer loop instead of only the inner one[39;00m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// The for/in statement allows iteration over properties of an object.[39m
|
||||
[38;5;246;03m// The for/in statement allows iteration over properties of an object.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mdescription[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m""[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mperson[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m[38;5;252mfname[39m[38;5;252m:[39m[38;5;214m"Paul"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mlname[39m[38;5;252m:[39m[38;5;214m"Ken"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mage[39m[38;5;252m:[39m[38;5;67m18[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mperson[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mdescription[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;252mperson[39m[38;5;252m[[39m[38;5;252mx[39m[38;5;252m][39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m" "[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246m// description = 'Paul Ken 18 '[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246;03m// description = 'Paul Ken 18 '[39;00m
|
||||
|
||||
[38;5;246m// The for/of statement allows iteration over iterable objects (including the built-in String, [39m
|
||||
[38;5;246m// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, [39m
|
||||
[38;5;246m// and user-defined iterables).[39m
|
||||
[38;5;246;03m// The for/of statement allows iteration over iterable objects (including the built-in String, [39;00m
|
||||
[38;5;246;03m// Array, e.g. the Array-like arguments or NodeList objects, TypedArray, Map and Set, [39;00m
|
||||
[38;5;246;03m// and user-defined iterables).[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyPets[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m""[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mpets[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m"cat"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"dog"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"hamster"[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"hedgehog"[39m[38;5;252m][39m[38;5;252m;[39m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mpet[39m[38;5;252m [39m[38;5;70;01mof[39;00m[38;5;252m [39m[38;5;252mpets[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyPets[39m[38;5;252m [39m[38;5;252m+=[39m[38;5;252m [39m[38;5;252mpet[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m" "[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246m// myPets = 'cat dog hamster hedgehog '[39m
|
||||
[38;5;252m}[39m[38;5;252m [39m[38;5;246;03m// myPets = 'cat dog hamster hedgehog '[39;00m
|
||||
|
||||
[38;5;246m// && is logical and, || is logical or[39m
|
||||
[38;5;246;03m// && is logical and, || is logical or[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mhouse[39m[38;5;252m.[39m[38;5;252msize[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"big"[39m[38;5;252m [39m[38;5;252m&&[39m[38;5;252m [39m[38;5;252mhouse[39m[38;5;252m.[39m[38;5;252mcolour[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"blue"[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mhouse[39m[38;5;252m.[39m[38;5;252mcontains[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"bear"[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mcolour[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"red"[39m[38;5;252m [39m[38;5;252m||[39m[38;5;252m [39m[38;5;252mcolour[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;214m"blue"[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// colour is either red or blue[39m
|
||||
[38;5;252m [39m[38;5;246;03m// colour is either red or blue[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// && and || "short circuit", which is useful for setting default values.[39m
|
||||
[38;5;246;03m// && and || "short circuit", which is useful for setting default values.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252motherName[39m[38;5;252m [39m[38;5;252m||[39m[38;5;252m [39m[38;5;214m"default"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// The `switch` statement checks for equality with `===`.[39m
|
||||
[38;5;246m// Use 'break' after each case[39m
|
||||
[38;5;246m// or the cases after the correct one will be executed too.[39m
|
||||
[38;5;246;03m// The `switch` statement checks for equality with `===`.[39;00m
|
||||
[38;5;246;03m// Use 'break' after each case[39;00m
|
||||
[38;5;246;03m// or the cases after the correct one will be executed too.[39;00m
|
||||
[38;5;252mgrade[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m'B'[39m[38;5;252m;[39m
|
||||
[38;5;70;01mswitch[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mgrade[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mcase[39;00m[38;5;252m [39m[38;5;214m'A'[39m[38;5;252m:[39m
|
||||
@ -282,172 +282,172 @@
|
||||
[38;5;252m}[39m
|
||||
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 4. Functions, Scope and Closures[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 4. Functions, Scope and Closures[39;00m
|
||||
|
||||
[38;5;246m// JavaScript functions are declared with the `function` keyword.[39m
|
||||
[38;5;246;03m// JavaScript functions are declared with the `function` keyword.[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252mthing[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mthing[39m[38;5;252m.[39m[38;5;252mtoUpperCase[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;214m"foo"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "FOO"[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;214m"foo"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "FOO"[39;00m
|
||||
|
||||
[38;5;246m// Note that the value to be returned must start on the same line as the[39m
|
||||
[38;5;246m// `return` keyword, otherwise you'll always return `undefined` due to[39m
|
||||
[38;5;246m// automatic semicolon insertion. Watch out for this when using Allman style.[39m
|
||||
[38;5;246;03m// Note that the value to be returned must start on the same line as the[39;00m
|
||||
[38;5;246;03m// `return` keyword, otherwise you'll always return `undefined` due to[39;00m
|
||||
[38;5;246;03m// automatic semicolon insertion. Watch out for this when using Allman style.[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;246m// <- semicolon automatically inserted here[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;246;03m// <- semicolon automatically inserted here[39;00m
|
||||
[38;5;252m [39m[38;5;252m{[39m[38;5;252mthisIsAn[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m'object literal'[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m// JavaScript functions are first class objects, so they can be reassigned to[39m
|
||||
[38;5;246m// different variable names and passed to other functions as arguments - for[39m
|
||||
[38;5;246m// example, when supplying an event handler:[39m
|
||||
[38;5;246;03m// JavaScript functions are first class objects, so they can be reassigned to[39;00m
|
||||
[38;5;246;03m// different variable names and passed to other functions as arguments - for[39;00m
|
||||
[38;5;246;03m// example, when supplying an event handler:[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// this code will be called in 5 seconds' time[39m
|
||||
[38;5;252m [39m[38;5;246;03m// this code will be called in 5 seconds' time[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252msetTimeout[39m[38;5;252m([39m[38;5;252mmyFunction[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;246m// Note: setTimeout isn't part of the JS language, but is provided by browsers[39m
|
||||
[38;5;246m// and Node.js.[39m
|
||||
[38;5;246;03m// Note: setTimeout isn't part of the JS language, but is provided by browsers[39;00m
|
||||
[38;5;246;03m// and Node.js.[39;00m
|
||||
|
||||
[38;5;246m// Another function provided by browsers is setInterval[39m
|
||||
[38;5;246;03m// Another function provided by browsers is setInterval[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252mmyFunction[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// this code will be called every 5 seconds[39m
|
||||
[38;5;252m [39m[38;5;246;03m// this code will be called every 5 seconds[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252msetInterval[39m[38;5;252m([39m[38;5;252mmyFunction[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// Function objects don't even have to be declared with a name - you can write[39m
|
||||
[38;5;246m// an anonymous function definition directly into the arguments of another.[39m
|
||||
[38;5;246;03m// Function objects don't even have to be declared with a name - you can write[39;00m
|
||||
[38;5;246;03m// an anonymous function definition directly into the arguments of another.[39;00m
|
||||
[38;5;252msetTimeout[39m[38;5;252m([39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// this code will be called in 5 seconds' time[39m
|
||||
[38;5;252m [39m[38;5;246;03m// this code will be called in 5 seconds' time[39;00m
|
||||
[38;5;252m}[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246m// JavaScript has function scope; functions get their own scope but other blocks[39m
|
||||
[38;5;246m// do not.[39m
|
||||
[38;5;246;03m// JavaScript has function scope; functions get their own scope but other blocks[39;00m
|
||||
[38;5;246;03m// do not.[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mtrue[39;00m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252mi[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5 - not undefined as you'd expect in a block-scoped language[39m
|
||||
[38;5;252mi[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5 - not undefined as you'd expect in a block-scoped language[39;00m
|
||||
|
||||
[38;5;246m// This has led to a common pattern of "immediately-executing anonymous[39m
|
||||
[38;5;246m// functions", which prevent temporary variables from leaking into the global[39m
|
||||
[38;5;246m// scope.[39m
|
||||
[38;5;246;03m// This has led to a common pattern of "immediately-executing anonymous[39;00m
|
||||
[38;5;246;03m// functions", which prevent temporary variables from leaking into the global[39;00m
|
||||
[38;5;246;03m// scope.[39;00m
|
||||
[38;5;252m([39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mtemporary[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// We can access the global scope by assigning to the "global object", which[39m
|
||||
[38;5;252m [39m[38;5;246m// in a web browser is always `window`. The global object may have a[39m
|
||||
[38;5;252m [39m[38;5;246m// different name in non-browser environments such as Node.js.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// We can access the global scope by assigning to the "global object", which[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// in a web browser is always `window`. The global object may have a[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// different name in non-browser environments such as Node.js.[39;00m
|
||||
[38;5;252m [39m[38;5;31mwindow[39m[38;5;252m.[39m[38;5;252mpermanent[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m10[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m)[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mtemporary[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// raises ReferenceError[39m
|
||||
[38;5;252mpermanent[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 10[39m
|
||||
[38;5;252mtemporary[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// raises ReferenceError[39;00m
|
||||
[38;5;252mpermanent[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 10[39;00m
|
||||
|
||||
[38;5;246m// One of JavaScript's most powerful features is closures. If a function is[39m
|
||||
[38;5;246m// defined inside another function, the inner function has access to all the[39m
|
||||
[38;5;246m// outer function's variables, even after the outer function exits.[39m
|
||||
[38;5;246;03m// One of JavaScript's most powerful features is closures. If a function is[39;00m
|
||||
[38;5;246;03m// defined inside another function, the inner function has access to all the[39;00m
|
||||
[38;5;246;03m// outer function's variables, even after the outer function exits.[39;00m
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252msayHelloInFiveSeconds[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mprompt[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"Hello, "[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"!"[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// Inner functions are put in the local scope by default, as if they were[39m
|
||||
[38;5;252m [39m[38;5;246m// declared with `var`.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// Inner functions are put in the local scope by default, as if they were[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// declared with `var`.[39;00m
|
||||
[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252minner[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252malert[39m[38;5;252m([39m[38;5;252mprompt[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m [39m[38;5;252msetTimeout[39m[38;5;252m([39m[38;5;252minner[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m5000[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will[39m
|
||||
[38;5;252m [39m[38;5;246m// exit immediately, and setTimeout will call inner afterwards. However,[39m
|
||||
[38;5;252m [39m[38;5;246m// because inner is "closed over" sayHelloInFiveSeconds, inner still has[39m
|
||||
[38;5;252m [39m[38;5;246m// access to the `prompt` variable when it is finally called.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// exit immediately, and setTimeout will call inner afterwards. However,[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// because inner is "closed over" sayHelloInFiveSeconds, inner still has[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// access to the `prompt` variable when it is finally called.[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;252msayHelloInFiveSeconds[39m[38;5;252m([39m[38;5;214m"Adam"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// will open a popup with "Hello, Adam!" in 5s[39m
|
||||
[38;5;252msayHelloInFiveSeconds[39m[38;5;252m([39m[38;5;214m"Adam"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// will open a popup with "Hello, Adam!" in 5s[39;00m
|
||||
|
||||
[38;5;246m///////////////////////////////////[39m
|
||||
[38;5;246m// 5. More about Objects; Constructors and Prototypes[39m
|
||||
[38;5;246;03m///////////////////////////////////[39;00m
|
||||
[38;5;246;03m// 5. More about Objects; Constructors and Prototypes[39;00m
|
||||
|
||||
[38;5;246m// Objects can contain functions.[39m
|
||||
[38;5;246;03m// Objects can contain functions.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyFunc[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;214m"Hello world!"[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world!"[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world!"[39;00m
|
||||
|
||||
[38;5;246m// When functions attached to an object are called, they can access the object[39m
|
||||
[38;5;246m// they're attached to using the `this` keyword.[39m
|
||||
[38;5;246;03m// When functions attached to an object are called, they can access the object[39;00m
|
||||
[38;5;246;03m// they're attached to using the `this` keyword.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyString[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"Hello world!"[39m[38;5;252m,[39m
|
||||
[38;5;252m [39m[38;5;252mmyFunc[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyString[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello world!"[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello world!"[39;00m
|
||||
|
||||
[38;5;246m// What this is set to has to do with how the function is called, not where[39m
|
||||
[38;5;246m// it's defined. So, our function doesn't work if it isn't called in the[39m
|
||||
[38;5;246m// context of the object.[39m
|
||||
[38;5;246;03m// What this is set to has to do with how the function is called, not where[39;00m
|
||||
[38;5;246;03m// it's defined. So, our function doesn't work if it isn't called in the[39;00m
|
||||
[38;5;246;03m// context of the object.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m;[39m
|
||||
[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = undefined[39m
|
||||
[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = undefined[39;00m
|
||||
|
||||
[38;5;246m// Inversely, a function can be assigned to the object and gain access to it[39m
|
||||
[38;5;246m// through `this`, even if it wasn't attached when it was defined.[39m
|
||||
[38;5;246;03m// Inversely, a function can be assigned to the object and gain access to it[39;00m
|
||||
[38;5;246;03m// through `this`, even if it wasn't attached when it was defined.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyOtherFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyString[39m[38;5;252m.[39m[38;5;252mtoUpperCase[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyOtherFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyOtherFunc[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyOtherFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "HELLO WORLD!"[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyOtherFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "HELLO WORLD!"[39;00m
|
||||
|
||||
[38;5;246m// We can also specify a context for a function to execute in when we invoke it[39m
|
||||
[38;5;246m// using `call` or `apply`.[39m
|
||||
[38;5;246;03m// We can also specify a context for a function to execute in when we invoke it[39;00m
|
||||
[38;5;246;03m// using `call` or `apply`.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252manotherFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252ms[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyString[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252ms[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mcall[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m" And Hello Moon!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello World! And Hello Moon!"[39m
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mcall[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m" And Hello Moon!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello World! And Hello Moon!"[39;00m
|
||||
|
||||
[38;5;246m// The `apply` function is nearly identical, but takes an array for an argument[39m
|
||||
[38;5;246m// list.[39m
|
||||
[38;5;246;03m// The `apply` function is nearly identical, but takes an array for an argument[39;00m
|
||||
[38;5;246;03m// list.[39;00m
|
||||
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m" And Hello Sun!"[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello World! And Hello Sun!"[39m
|
||||
[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;214m" And Hello Sun!"[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello World! And Hello Sun!"[39;00m
|
||||
|
||||
[38;5;246m// This is useful when working with a function that accepts a sequence of[39m
|
||||
[38;5;246m// arguments and you want to pass an array.[39m
|
||||
[38;5;246;03m// This is useful when working with a function that accepts a sequence of[39;00m
|
||||
[38;5;246;03m// arguments and you want to pass an array.[39;00m
|
||||
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 6[39m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = NaN (uh-oh!)[39m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;31mMath[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 6[39m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 6[39;00m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m([39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = NaN (uh-oh!)[39;00m
|
||||
[38;5;31mMath[39m[38;5;252m.[39m[38;5;252mmin[39m[38;5;252m.[39m[38;5;252mapply[39m[38;5;252m([39m[38;5;31mMath[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m[[39m[38;5;67m42[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m27[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 6[39;00m
|
||||
|
||||
[38;5;246m// But, `call` and `apply` are only temporary. When we want it to stick, we can[39m
|
||||
[38;5;246m// use `bind`.[39m
|
||||
[38;5;246;03m// But, `call` and `apply` are only temporary. When we want it to stick, we can[39;00m
|
||||
[38;5;246;03m// use `bind`.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mboundFunc[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252manotherFunc[39m[38;5;252m.[39m[38;5;252mbind[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mboundFunc[39m[38;5;252m([39m[38;5;214m" And Hello Saturn!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "Hello World! And Hello Saturn!"[39m
|
||||
[38;5;252mboundFunc[39m[38;5;252m([39m[38;5;214m" And Hello Saturn!"[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "Hello World! And Hello Saturn!"[39;00m
|
||||
|
||||
[38;5;246m// `bind` can also be used to partially apply (curry) a function.[39m
|
||||
[38;5;246;03m// `bind` can also be used to partially apply (curry) a function.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mproduct[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252ma[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252mb[39m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252ma[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;252mb[39m[38;5;252m;[39m[38;5;252m [39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mdoubler[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mproduct[39m[38;5;252m.[39m[38;5;252mbind[39m[38;5;252m([39m[38;5;70;01mthis[39;00m[38;5;252m,[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mdoubler[39m[38;5;252m([39m[38;5;67m8[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 16[39m
|
||||
[38;5;252mdoubler[39m[38;5;252m([39m[38;5;67m8[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 16[39;00m
|
||||
|
||||
[38;5;246m// When you call a function with the `new` keyword, a new object is created, and[39m
|
||||
[38;5;246m// made available to the function via the `this` keyword. Functions designed to be[39m
|
||||
[38;5;246m// called like that are called constructors.[39m
|
||||
[38;5;246;03m// When you call a function with the `new` keyword, a new object is created, and[39;00m
|
||||
[38;5;246;03m// made available to the function via the `this` keyword. Functions designed to be[39;00m
|
||||
[38;5;246;03m// called like that are called constructors.[39;00m
|
||||
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = {myNumber: 5}[39m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5[39m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = {myNumber: 5}[39;00m
|
||||
[38;5;252mmyNewObj[39m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5[39;00m
|
||||
|
||||
[38;5;246m// Unlike most other popular object-oriented languages, JavaScript has no[39m
|
||||
[38;5;246m// concept of 'instances' created from 'class' blueprints; instead, JavaScript[39m
|
||||
[38;5;246m// combines instantiation and inheritance into a single concept: a 'prototype'.[39m
|
||||
[38;5;246;03m// Unlike most other popular object-oriented languages, JavaScript has no[39;00m
|
||||
[38;5;246;03m// concept of 'instances' created from 'class' blueprints; instead, JavaScript[39;00m
|
||||
[38;5;246;03m// combines instantiation and inheritance into a single concept: a 'prototype'.[39;00m
|
||||
|
||||
[38;5;246m// Every JavaScript object has a 'prototype'. When you go to access a property[39m
|
||||
[38;5;246m// on an object that doesn't exist on the actual object, the interpreter will[39m
|
||||
[38;5;246m// look at its prototype.[39m
|
||||
[38;5;246;03m// Every JavaScript object has a 'prototype'. When you go to access a property[39;00m
|
||||
[38;5;246;03m// on an object that doesn't exist on the actual object, the interpreter will[39;00m
|
||||
[38;5;246;03m// look at its prototype.[39;00m
|
||||
|
||||
[38;5;246m// Some JS implementations let you access an object's prototype on the magic[39m
|
||||
[38;5;246m// property `__proto__`. While this is useful for explaining prototypes it's not[39m
|
||||
[38;5;246m// part of the standard; we'll get to standard ways of using prototypes later.[39m
|
||||
[38;5;246;03m// Some JS implementations let you access an object's prototype on the magic[39;00m
|
||||
[38;5;246;03m// property `__proto__`. While this is useful for explaining prototypes it's not[39;00m
|
||||
[38;5;246;03m// part of the standard; we'll get to standard ways of using prototypes later.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyString[39m[38;5;252m:[39m[38;5;252m [39m[38;5;214m"Hello world!"[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
@ -459,58 +459,58 @@
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252m__proto__[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mmyPrototype[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 42[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 42[39;00m
|
||||
|
||||
[38;5;246m// This works for functions, too.[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "hello world!"[39m
|
||||
[38;5;246;03m// This works for functions, too.[39;00m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyFunc[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "hello world!"[39;00m
|
||||
|
||||
[38;5;246m// Of course, if your property isn't on your prototype, the prototype's[39m
|
||||
[38;5;246m// prototype is searched, and so on.[39m
|
||||
[38;5;246;03m// Of course, if your property isn't on your prototype, the prototype's[39;00m
|
||||
[38;5;246;03m// prototype is searched, and so on.[39;00m
|
||||
[38;5;252mmyPrototype[39m[38;5;252m.[39m[38;5;252m__proto__[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyBoolean[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mtrue[39;00m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyBoolean[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmyBoolean[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// There's no copying involved here; each object stores a reference to its[39m
|
||||
[38;5;246m// prototype. This means we can alter the prototype and our changes will be[39m
|
||||
[38;5;246m// reflected everywhere.[39m
|
||||
[38;5;246;03m// There's no copying involved here; each object stores a reference to its[39;00m
|
||||
[38;5;246;03m// prototype. This means we can alter the prototype and our changes will be[39;00m
|
||||
[38;5;246;03m// reflected everywhere.[39;00m
|
||||
[38;5;252mmyPrototype[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m43[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 43[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 43[39;00m
|
||||
|
||||
[38;5;246m// The for/in statement allows iteration over properties of an object,[39m
|
||||
[38;5;246m// walking up the prototype chain until it sees a null prototype.[39m
|
||||
[38;5;246;03m// The for/in statement allows iteration over properties of an object,[39;00m
|
||||
[38;5;246;03m// walking up the prototype chain until it sees a null prototype.[39;00m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mconsole[39m[38;5;252m.[39m[38;5;252mlog[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;252mx[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;246m///prints:[39m
|
||||
[38;5;246m// Hello world![39m
|
||||
[38;5;246m// 43[39m
|
||||
[38;5;246m// [Function: myFunc][39m
|
||||
[38;5;246m// true[39m
|
||||
[38;5;246;03m///prints:[39;00m
|
||||
[38;5;246;03m// Hello world![39;00m
|
||||
[38;5;246;03m// 43[39;00m
|
||||
[38;5;246;03m// [Function: myFunc][39;00m
|
||||
[38;5;246;03m// true[39;00m
|
||||
|
||||
[38;5;246m// To only consider properties attached to the object itself[39m
|
||||
[38;5;246m// and not its prototypes, use the `hasOwnProperty()` check.[39m
|
||||
[38;5;246;03m// To only consider properties attached to the object itself[39;00m
|
||||
[38;5;246;03m// and not its prototypes, use the `hasOwnProperty()` check.[39;00m
|
||||
[38;5;70;01mfor[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mx[39m[38;5;252m [39m[38;5;70;01min[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mhasOwnProperty[39m[38;5;252m([39m[38;5;252mx[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mconsole[39m[38;5;252m.[39m[38;5;252mlog[39m[38;5;252m([39m[38;5;252mmyObj[39m[38;5;252m[[39m[38;5;252mx[39m[38;5;252m][39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m
|
||||
[38;5;246m///prints:[39m
|
||||
[38;5;246m// Hello world![39m
|
||||
[38;5;246;03m///prints:[39;00m
|
||||
[38;5;246;03m// Hello world![39;00m
|
||||
|
||||
[38;5;246m// We mentioned that `__proto__` was non-standard, and there's no standard way to[39m
|
||||
[38;5;246m// change the prototype of an existing object. However, there are two ways to[39m
|
||||
[38;5;246m// create a new object with a given prototype.[39m
|
||||
[38;5;246;03m// We mentioned that `__proto__` was non-standard, and there's no standard way to[39;00m
|
||||
[38;5;246;03m// change the prototype of an existing object. However, there are two ways to[39;00m
|
||||
[38;5;246;03m// create a new object with a given prototype.[39;00m
|
||||
|
||||
[38;5;246m// The first is Object.create, which is a recent addition to JS, and therefore[39m
|
||||
[38;5;246m// not available in all implementations yet.[39m
|
||||
[38;5;246;03m// The first is Object.create, which is a recent addition to JS, and therefore[39;00m
|
||||
[38;5;246;03m// not available in all implementations yet.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m([39m[38;5;252mmyPrototype[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 43[39m
|
||||
[38;5;252mmyObj[39m[38;5;252m.[39m[38;5;252mmeaningOfLife[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 43[39;00m
|
||||
|
||||
[38;5;246m// The second way, which works anywhere, has to do with constructors.[39m
|
||||
[38;5;246m// Constructors have a property called prototype. This is *not* the prototype of[39m
|
||||
[38;5;246m// the constructor function itself; instead, it's the prototype that new objects[39m
|
||||
[38;5;246m// are given when they're created with that constructor and the new keyword.[39m
|
||||
[38;5;246;03m// The second way, which works anywhere, has to do with constructors.[39;00m
|
||||
[38;5;246;03m// Constructors have a property called prototype. This is *not* the prototype of[39;00m
|
||||
[38;5;246;03m// the constructor function itself; instead, it's the prototype that new objects[39;00m
|
||||
[38;5;246;03m// are given when they're created with that constructor and the new keyword.[39;00m
|
||||
[38;5;252mMyConstructor[39m[38;5;252m.[39m[38;5;252mprototype[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m:[39m[38;5;252m [39m[38;5;67m5[39m[38;5;252m,[39m
|
||||
[38;5;252m [39m[38;5;252mgetMyNumber[39m[38;5;252m:[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
@ -518,47 +518,89 @@
|
||||
[38;5;252m [39m[38;5;252m}[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyNewObj2[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mMyConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 5[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 5[39;00m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m6[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 6[39m
|
||||
[38;5;252mmyNewObj2[39m[38;5;252m.[39m[38;5;252mgetMyNumber[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 6[39;00m
|
||||
|
||||
[38;5;246m// Built-in types like strings and numbers also have constructors that create[39m
|
||||
[38;5;246m// equivalent wrapper objects.[39m
|
||||
[38;5;246;03m// Built-in types like strings and numbers also have constructors that create[39;00m
|
||||
[38;5;246;03m// equivalent wrapper objects.[39;00m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m12[39m[38;5;252m;[39m
|
||||
[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;31mNumber[39m[38;5;252m([39m[38;5;67m12[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = true[39m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m==[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = true[39;00m
|
||||
|
||||
[38;5;246m// Except, they aren't exactly equivalent.[39m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 'number'[39m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = 'object'[39m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = false[39m
|
||||
[38;5;246;03m// Except, they aren't exactly equivalent.[39;00m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumber[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 'number'[39;00m
|
||||
[38;5;70;01mtypeof[39;00m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = 'object'[39;00m
|
||||
[38;5;252mmyNumber[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;252mmyNumberObj[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = false[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// This code won't execute, because 0 is falsy.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// This code won't execute, because 0 is falsy.[39;00m
|
||||
[38;5;252m}[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;31mNumber[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// This code will execute, because wrapped numbers are objects, and objects[39m
|
||||
[38;5;252m [39m[38;5;246m// are always truthy.[39m
|
||||
[38;5;252m [39m[38;5;246;03m// This code will execute, because wrapped numbers are objects, and objects[39;00m
|
||||
[38;5;252m [39m[38;5;246;03m// are always truthy.[39;00m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246m// However, the wrapper objects and the regular builtins share a prototype, so[39m
|
||||
[38;5;246m// you can actually add functionality to a string, for instance.[39m
|
||||
[38;5;246;03m// However, the wrapper objects and the regular builtins share a prototype, so[39;00m
|
||||
[38;5;246;03m// you can actually add functionality to a string, for instance.[39;00m
|
||||
[38;5;31mString[39m[38;5;252m.[39m[38;5;252mprototype[39m[38;5;252m.[39m[38;5;252mfirstCharacter[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mthis[39;00m[38;5;252m.[39m[38;5;252mcharAt[39m[38;5;252m([39m[38;5;67m0[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;214m"abc"[39m[38;5;252m.[39m[38;5;252mfirstCharacter[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246m// = "a"[39m
|
||||
[38;5;214m"abc"[39m[38;5;252m.[39m[38;5;252mfirstCharacter[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// = "a"[39;00m
|
||||
|
||||
[38;5;246m// This fact is often used in "polyfilling", which is implementing newer[39m
|
||||
[38;5;246m// features of JavaScript in an older subset of JavaScript, so that they can be[39m
|
||||
[38;5;246m// used in older environments such as outdated browsers.[39m
|
||||
[38;5;246;03m// This fact is often used in "polyfilling", which is implementing newer[39;00m
|
||||
[38;5;246;03m// features of JavaScript in an older subset of JavaScript, so that they can be[39;00m
|
||||
[38;5;246;03m// used in older environments such as outdated browsers.[39;00m
|
||||
|
||||
[38;5;246m// For instance, we mentioned that Object.create isn't yet available in all[39m
|
||||
[38;5;246m// implementations, but we can still use it with this polyfill:[39m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m [39m[38;5;246m// don't overwrite it if it exists[39m
|
||||
[38;5;246;03m// For instance, we mentioned that Object.create isn't yet available in all[39;00m
|
||||
[38;5;246;03m// implementations, but we can still use it with this polyfill:[39;00m
|
||||
[38;5;70;01mif[39;00m[38;5;252m [39m[38;5;252m([39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;70;01mundefined[39;00m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m [39m[38;5;246;03m// don't overwrite it if it exists[39;00m
|
||||
[38;5;252m [39m[38;5;31mObject[39m[38;5;252m.[39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252mproto[39m[38;5;252m)[39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;246m// make a temporary constructor with the right prototype[39m
|
||||
[38;5;252m [39m[38;5;246;03m// make a temporary constructor with the right prototype[39;00m
|
||||
[38;5;252m [39m[38;5;70;01mvar[39;00m[38;5;252m [39m[38;5;252mConstructor[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;70;01mfunction[39;00m[38;5;252m([39m[38;5;252m)[39m[38;5;252m{[39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252mConstructor[39m[38;5;252m.[39m[38;5;252mprototype[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mproto[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;246m// then use it to create a new, appropriately-prototyped object[39m
|
||||
[38;5;252m [39m[38;5;246;03m// then use it to create a new, appropriately-prototyped object[39;00m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;70;01mnew[39;00m[38;5;252m [39m[38;5;252mConstructor[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
[38;5;252m [39m[38;5;252m}[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m
|
||||
|
||||
[38;5;246;03m// ES6 Additions[39;00m
|
||||
|
||||
[38;5;246;03m// The "let" keyword allows you to define variables in a lexical scope, [39;00m
|
||||
[38;5;246;03m// as opposed to a block scope like the var keyword does.[39;00m
|
||||
[38;5;70;01mlet[39;00m[38;5;252m [39m[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"Billy"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246;03m// Variables defined with let can be reassigned new values.[39;00m
|
||||
[38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"William"[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246;03m// The "const" keyword allows you to define a variable in a lexical scope[39;00m
|
||||
[38;5;246;03m// like with let, but you cannot reassign the value once one has been assigned.[39;00m
|
||||
|
||||
[38;5;70;01mconst[39;00m[38;5;252m [39m[38;5;252mpi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m3.14[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;252mpi[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m4.13[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// You cannot do this.[39;00m
|
||||
|
||||
[38;5;246;03m// There is a new syntax for functions in ES6 known as "lambda syntax".[39;00m
|
||||
[38;5;246;03m// This allows functions to be defined in a lexical scope like with variables[39;00m
|
||||
[38;5;246;03m// defined by const and let. [39;00m
|
||||
|
||||
[38;5;70;01mconst[39;00m[38;5;252m [39m[38;5;252misEven[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;252mnumber[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m=>[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mnumber[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;252misEven[39m[38;5;252m([39m[38;5;67m7[39m[38;5;252m)[39m[38;5;252m;[39m[38;5;252m [39m[38;5;246;03m// false[39;00m
|
||||
|
||||
[38;5;246;03m// The "equivalent" of this function in the traditional syntax would look like this:[39;00m
|
||||
|
||||
[38;5;70;01mfunction[39;00m[38;5;252m [39m[38;5;252misEven[39m[38;5;252m([39m[38;5;252mnumber[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mnumber[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;252m===[39m[38;5;252m [39m[38;5;67m0[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;246;03m// I put the word "equivalent" in double quotes because a function defined[39;00m
|
||||
[38;5;246;03m// using the lambda syntax cannnot be called before the definition.[39;00m
|
||||
[38;5;246;03m// The following is an example of invalid usage:[39;00m
|
||||
|
||||
[38;5;252madd[39m[38;5;252m([39m[38;5;67m1[39m[38;5;252m,[39m[38;5;252m [39m[38;5;67m8[39m[38;5;252m)[39m[38;5;252m;[39m
|
||||
|
||||
[38;5;70;01mconst[39;00m[38;5;252m [39m[38;5;252madd[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252m([39m[38;5;252mfirstNumber[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252msecondNumber[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m=>[39m[38;5;252m [39m[38;5;252m{[39m
|
||||
[38;5;252m [39m[38;5;70;01mreturn[39;00m[38;5;252m [39m[38;5;252mfirstNumber[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;252msecondNumber[39m[38;5;252m;[39m
|
||||
[38;5;252m}[39m[38;5;252m;[39m
|
||||
|
@ -1,17 +1,17 @@
|
||||
# Displays everything in the target directory
|
||||
ls path/to/the/target/directory
|
||||
# To display everything in <dir>, excluding hidden files:
|
||||
ls <dir>
|
||||
|
||||
# Displays everything including hidden files
|
||||
ls -a
|
||||
# To display everything in <dir>, including hidden files:
|
||||
ls -a <dir>
|
||||
|
||||
# Displays all files, along with the size (with unit suffixes) and timestamp
|
||||
ls -lh
|
||||
# To display all files, along with the size (with unit suffixes) and timestamp
|
||||
ls -lh <dir>
|
||||
|
||||
# Display files, sorted by size
|
||||
ls -S
|
||||
# To display files, sorted by size:
|
||||
ls -S <dir>
|
||||
|
||||
# Display directories only
|
||||
ls -d */
|
||||
# To display directories only:
|
||||
ls -d */ <dir>
|
||||
|
||||
# Display directories only, include hidden
|
||||
ls -d .*/ */
|
||||
# To display directories only, include hidden:
|
||||
ls -d .*/ */ <dir>
|
||||
|
@ -1,54 +1,54 @@
|
||||
[38;5;246m# Create a btrfs file system on /dev/sdb, /dev/sdc, and /dev/sdd[39m
|
||||
[38;5;246;03m# Create a btrfs file system on /dev/sdb, /dev/sdc, and /dev/sdd[39;00m
|
||||
[38;5;252mmkfs.btrfs[39m[38;5;252m [39m[38;5;252m/dev/sdb[39m[38;5;252m [39m[38;5;252m/dev/sdc[39m[38;5;252m [39m[38;5;252m/dev/sdd[39m
|
||||
|
||||
[38;5;246m# btrfs with just one hard drive, metadata not redundant[39m
|
||||
[38;5;246m# (this is danegerous: if your metadata is lost, your data is lost as well)[39m
|
||||
[38;5;246;03m# btrfs with just one hard drive, metadata not redundant[39;00m
|
||||
[38;5;246;03m# (this is danegerous: if your metadata is lost, your data is lost as well)[39;00m
|
||||
[38;5;252mmkfs.btrfs[39m[38;5;252m [39m[38;5;252m-m[39m[38;5;252m [39m[38;5;252msingle[39m[38;5;252m [39m[38;5;252m/dev/sdb[39m
|
||||
|
||||
[38;5;246m# data to be redundant and metadata to be non-redundant:[39m
|
||||
[38;5;246;03m# data to be redundant and metadata to be non-redundant:[39;00m
|
||||
[38;5;252mmkfs.btrfs[39m[38;5;252m [39m[38;5;252m-m[39m[38;5;252m [39m[38;5;252mraid0[39m[38;5;252m [39m[38;5;252m-d[39m[38;5;252m [39m[38;5;252mraid1[39m[38;5;252m [39m[38;5;252m/dev/sdb[39m[38;5;252m [39m[38;5;252m/dev/sdc[39m[38;5;252m [39m[38;5;252m/dev/sdd[39m
|
||||
|
||||
[38;5;246m# both data and metadata to be redundan[39m
|
||||
[38;5;246;03m# both data and metadata to be redundan[39;00m
|
||||
[38;5;252mmkfs.btrfs[39m[38;5;252m [39m[38;5;252m-d[39m[38;5;252m [39m[38;5;252mraid1[39m[38;5;252m [39m[38;5;252m/dev/sdb[39m[38;5;252m [39m[38;5;252m/dev/sdc[39m[38;5;252m [39m[38;5;252m/dev/sdd[39m
|
||||
|
||||
[38;5;246m# To get a list of all btrfs file systems[39m
|
||||
[38;5;246;03m# To get a list of all btrfs file systems[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252mfilesystem[39m[38;5;252m [39m[38;5;252mshow[39m
|
||||
|
||||
[38;5;246m# detailed df for a fileesystem (mounted in /mnt)[39m
|
||||
[38;5;246;03m# detailed df for a fileesystem (mounted in /mnt)[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252mfilesystem[39m[38;5;252m [39m[38;5;252mdf[39m[38;5;252m [39m[38;5;252m/mnt[39m
|
||||
|
||||
[38;5;246m# resize btrfs online (-2g decreases, +2g increases)[39m
|
||||
[38;5;246;03m# resize btrfs online (-2g decreases, +2g increases)[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252mfilesystem[39m[38;5;252m [39m[38;5;252mresize[39m[38;5;252m [39m[38;5;252m-2g[39m[38;5;252m [39m[38;5;252m/mnt[39m
|
||||
|
||||
[38;5;246m# use maximum space[39m
|
||||
[38;5;246;03m# use maximum space[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252mfilesystem[39m[38;5;252m [39m[38;5;252mresize[39m[38;5;252m [39m[38;5;252mmax[39m[38;5;252m [39m[38;5;252m/mnt[39m
|
||||
|
||||
[38;5;246m# add new device to a filesystem[39m
|
||||
[38;5;246;03m# add new device to a filesystem[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252mdevice[39m[38;5;252m [39m[38;5;252madd[39m[38;5;252m [39m[38;5;252m/dev/sdf[39m[38;5;252m [39m[38;5;252m/mnt[39m
|
||||
|
||||
[38;5;246m# remove devices from a filesystem[39m
|
||||
[38;5;246;03m# remove devices from a filesystem[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252mdevice[39m[38;5;252m [39m[38;5;252mdelete[39m[38;5;252m [39m[38;5;252mmissing[39m[38;5;252m [39m[38;5;252m/mnt[39m
|
||||
|
||||
[38;5;246m# create the subvolume /mnt/sv1 in the /mnt volume[39m
|
||||
[38;5;246;03m# create the subvolume /mnt/sv1 in the /mnt volume[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252msubvolume[39m[38;5;252m [39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m/mnt/sv1[39m
|
||||
|
||||
[38;5;246m# list subvolumes[39m
|
||||
[38;5;246;03m# list subvolumes[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252msubvolume[39m[38;5;252m [39m[38;5;252mlist[39m[38;5;252m [39m[38;5;252m/mnt[39m
|
||||
|
||||
[38;5;246m# mount subvolume without mounting the main filesystem[39m
|
||||
[38;5;246;03m# mount subvolume without mounting the main filesystem[39;00m
|
||||
[38;5;252mmount[39m[38;5;252m [39m[38;5;252m-o[39m[38;5;252m [39m[38;5;87msubvol[39m[38;5;252m=[39m[38;5;252msv1[39m[38;5;252m [39m[38;5;252m/dev/sdb[39m[38;5;252m [39m[38;5;252m/mnt[39m
|
||||
|
||||
[38;5;246m# delete subvolume[39m
|
||||
[38;5;246;03m# delete subvolume[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252msubvolume[39m[38;5;252m [39m[38;5;252mdelete[39m[38;5;252m [39m[38;5;252m/mnt/sv1[39m
|
||||
|
||||
[38;5;246m# taking snapshot of a subvolume[39m
|
||||
[38;5;246;03m# taking snapshot of a subvolume[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252msubvolume[39m[38;5;252m [39m[38;5;252msnapshot[39m[38;5;252m [39m[38;5;252m/mnt/sv1[39m[38;5;252m [39m[38;5;252m/mnt/sv1_snapshot[39m
|
||||
|
||||
[38;5;246m# taking snapshot of a file (copy file by reference)[39m
|
||||
[38;5;246;03m# taking snapshot of a file (copy file by reference)[39;00m
|
||||
[38;5;252mcp[39m[38;5;252m [39m[38;5;252m--reflink[39m[38;5;252m [39m[38;5;252m/mnt/sv1/test1[39m[38;5;252m [39m[38;5;252m/mnt/sv1/test3[39m
|
||||
|
||||
[38;5;246m# convert ext3/ext4 to btrfs[39m
|
||||
[38;5;246;03m# convert ext3/ext4 to btrfs[39;00m
|
||||
[38;5;252mbtrfs-convert[39m[38;5;252m [39m[38;5;252m/dev/sdb1[39m
|
||||
|
||||
[38;5;246m# convert btrfs to ext3/ext4[39m
|
||||
[38;5;246;03m# convert btrfs to ext3/ext4[39;00m
|
||||
[38;5;252mbtrfs-convert[39m[38;5;252m [39m[38;5;252m-r[39m[38;5;252m [39m[38;5;252m/dev/sdb1[39m
|
||||
|
@ -1,3 +1,3 @@
|
||||
[48;5;8m[24m btrfs [24m[0m
|
||||
[38;5;246m# create the subvolume /mnt/sv1 in the /mnt volume[39m
|
||||
[38;5;246;03m# create the subvolume /mnt/sv1 in the /mnt volume[39;00m
|
||||
[38;5;252mbtrfs[39m[38;5;252m [39m[38;5;252msubvolume[39m[38;5;252m [39m[38;5;252mcreate[39m[38;5;252m [39m[38;5;252m/mnt/sv1[39m
|
||||
|
@ -25,6 +25,7 @@ echo "Using PYTHON $python_version: $PYTHON"
|
||||
skip_online="${CHEATSH_TEST_SKIP_ONLINE:-NO}"
|
||||
test_standalone="${CHEATSH_TEST_STANDALONE:-YES}"
|
||||
show_details="${CHEATSH_TEST_SHOW_DETAILS:-YES}"
|
||||
update_tests_results="${CHEATSH_UPDATE_TESTS_RESULTS:-NO}"
|
||||
CHTSH_URL="${CHTSH_URL:-http://localhost:8002}"
|
||||
|
||||
TMP=$(mktemp /tmp/cht.sh.tests-XXXXXXXXXXXXXX)
|
||||
@ -83,19 +84,29 @@ while read -r number test_line; do
|
||||
fi
|
||||
|
||||
if ! diff -u3 --color=always results/"$number" "$TMP" > "$TMP2"; then
|
||||
if [ "$show_details" = YES ]; then
|
||||
cat "$TMP2"
|
||||
if [[ $CHEATSH_UPDATE_TESTS_RESULTS = NO ]]; then
|
||||
if [ "$show_details" = YES ]; then
|
||||
echo "$ CHEATSH_CACHE_TYPE=none python ../lib/standalone.py $test_line"
|
||||
cat "$TMP2"
|
||||
fi
|
||||
if grep -q "Internal Server Error" "$TMP2"; then
|
||||
[[ $TRAVIS == true ]] && docker logs chtsh
|
||||
fi
|
||||
echo "FAILED: [$number] $test_line"
|
||||
else
|
||||
cat "$TMP" > results/"$number"
|
||||
echo "UPDATED: [$number] $test_line"
|
||||
fi
|
||||
if grep -q "Internal Server Error" "$TMP2"; then
|
||||
[[ $TRAVIS == true ]] && docker logs chtsh
|
||||
fi
|
||||
echo "FAILED: [$number] $test_line"
|
||||
((failed++))
|
||||
fi
|
||||
((i++))
|
||||
done < "$TMP3"
|
||||
|
||||
echo TESTS/OK/FAILED "$i/$((i-failed))/$failed"
|
||||
if [[ $CHEATSH_UPDATE_TESTS_RESULTS = NO ]]; then
|
||||
echo TESTS/OK/FAILED "$i/$((i-failed))/$failed"
|
||||
else
|
||||
echo TESTS/OK/UPDATED "$i/$((i-failed))/$failed"
|
||||
fi
|
||||
|
||||
if [ "$failed" != 0 ]; then
|
||||
exit 1
|
||||
|
Loading…
Reference in New Issue
Block a user