Insert cell
Insert cell
Insert cell
scopesExample = {
// if we ignore the globalScopeExample = { } above...
var imGlobal = "global variable"
function localScope() {
var imLocal = "local variable"
imGlobal // global can be accessed in here.
}
localScope()
// global variable can be accessed over here but not local variable
imGlobal
imLocal
}
Insert cell
Insert cell
locallyScopedFunctions = {
function globalFunction() {
function localFunction() {
return "Hai from local function"
}
return "Hai from global function"
}
globalFunction()
localFunction()
}
Insert cell
Insert cell
{ //I know this seems like a far-fetched example, but bookmark this notebook for when we get to React
function makeKidsGetIceCream() {
var kid = {
name: "Random Child",
iceCream: null,
getIceCream: function(flavor) {
this.iceCream = flavor
return this.name + " got " + this.iceCream + "-flavored ice cream!"
}
}
return kid.getIceCream
}
var iceCreamGetter = makeKidsGetIceCream()
return iceCreamGetter("chocolate")
}
Insert cell
Insert cell
{ //I know this seems like a far-fetched example, but bookmark this notebook for when we get to React
function makeKidsGetIceCream() {
var kid = {
name: "Random Child",
iceCream: null,
getIceCream: function(flavor) {
this.iceCream = flavor
return this.name + " got " + this.iceCream + "-flavored ice cream!"
}
}
return kid.getIceCream.bind(kid) //notice that .bind() actually creates a new function
}
var iceCreamGetter = makeKidsGetIceCream()
return iceCreamGetter("chocolate")
}
Insert cell
Insert cell
(param1, param2) => {
//code here
return 'something'
}
Insert cell
Insert cell
Insert cell
param => {
//code here
return 'something'
}
Insert cell
Insert cell
() => {
//code here
return 'something'
}
Insert cell
Insert cell
() => 'something'
Insert cell
Insert cell
func = function() {
return this.attr;
}
Insert cell
funcA = () => this.attr
Insert cell
objContext = ({
attr: 'objectAttribute',
objFunc: func,
objFuncA: funcA
})
Insert cell
objContext.objFunc()
Insert cell
objContext.objFuncA()
Insert cell
func.bind(objContext)()
Insert cell
funcA.bind(objContext)()
Insert cell
Insert cell
fortune = (type='random') => {
var fortunes = ['you will be happy', 'you will be sad', 'you will eat pineapple']
if(type==='happy')
return 'Your '+ type +' fortune: ' + fortunes[0]
else if(type==='sad')
return 'Your '+ type +' fortune: ' + fortunes[1]
else
return 'Your '+ type +' fortune: ' + fortunes[Math.floor(Math.random() * 3)]
}
Insert cell
fortune()
Insert cell
Insert cell
mysteryFunction = ({bob=5, joe=10, mary=30}={}) => {
//some long and complicated code nobody can read
return bob*joe*mary
}
Insert cell
mysteryFunction()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more