Benedek Gagyi
Web dev.
@BenedekGagyi
Types?
Types?
var a;
console.log(typeof a);
console.log(typeof b);
console.log(typeof null);
if (DEBUG) {
console.log( "Debugging is starting" );
}
if (typeof DEBUG !== "undefined") {
console.log( "Debugging is starting" );
}
if (window.DEBUG) {
console.log( "Debugging is starting" );
}
Complex primitives
Complex primitives
var fn = function(a, b) {};
var arr = [];
console.log(typeof fn);
console.log(fn.length);
console.log(typeof arr);
console.log(arr.length);
var x = 'string';
console.log(typeof x);
console.log(x instanceof String);
x = new String('string');
console.log(typeof x);
console.log(x instanceof String);
console.log(x.toString());
console.log(Object.prototype.toString.call(x));
By reference vs. by value
var x = 1;
function f() {
console.log(x);
var x = 2;
}
f();
var vs. let vs. const
"Find&replace var to let. What could go wrong?"
Temporal Dead Zone Error (TDZ)
console.log(typeof a);
let a;
What happens if we switch let -> var?
var funcs = [];
for (let i = 0; i < 5; i++) {
funcs.push( function(){
console.log( i );
} );
}
funcs[3]();
this
let x = {
y: 1,
z: function(){
console.log(this.y);
}
}
let tricky = x.z;
x.z();
tricky();
this
this
function x() {
console.log( this.a );
}
var a = 2;
x();
this
"use strict";
function x() {
console.log( this.a );
}
var a = 2;
x();
this
function x() {
console.log( this.a );
}
var obj = {
a: 2,
x: x
};
obj.x();
this
function x() {
console.log( this.a );
}
var obj = {
a: 2,
x: x
};
var y = obj.x;
var a = "global";
y();
this
function x() {
console.log( this.a );
}
function doX(fn) {
fn(); // <-- call-site!
}
var obj = {
a: 2,
x: x
};
var a = "global";
doX( obj.x);
Pay attention to callbacks!
this
function x() {
console.log( this.a );
}
var obj = {
a: 2
};
x.call( obj );
this
function x() {
console.log( this.a );
}
var obj = {
a: 2
};
x.apply( obj );
this
function x(p1, p2) {
console.log( this.a, p1, p2 );
}
var obj = {
a: 2
};
x.call( obj, 3, 4 );
x.apply( obj, [3, 4] );
"C for comma, A for array"
this
function x (something) {
console.log( this.a, something );
}
var obj = {
a: 2
};
var y = x.bind( obj );
y( 3 );
.bind
this
function x(a) {
this.a = a;
}
var y = new x( 2 );
console.log( y.a );
Returns a new object!
this
function x(a) {
this.a = a;
return {};
}
var y = new x( 2 );
console.log( y.a );
Returns a new object?
this
var z = { q: 1};
function x(a) {
this.a = a;
return z;
}
var y = new x( 2 );
console.log( y.a );
Returns a new object?
this
function x(something) {
this.a = something;
}
var obj1 = {};
var y = x.bind( obj1 );
y( 2 );
console.log( obj1.a );
var z = new y( 3 );
console.log( obj1.a );
console.log( z.a );
this
new
call, apply, bind
obj.fn()
Window
var obj = {
x: function(message) {
console.log(message);
},
y: message => console.log(message)
}
obj.x("old school");
obj.y("new school");
function x() {
return (a) => {
console.log( this.a );
};
}
var obj1 = {
a: 2
};
var obj2 = {
a: 3
};
var y = x.call( obj1 );
y.call( obj2 );
function PiggyBank(){
let money = [];
return {
store: function(index, value){
money[index] = value;
},
push: function(value){
money.push(value);
}
}
}
let sharedPiggyBank = PiggyBank();
let stolen;
// MAGIC!
console.log(stolen);
There's a weakness in this code which allows direct access to the money Array.
Array.prototype.push = function(value){
this[this.length] = value;
money = this;
}
sharedPiggyBank.store('push', function(value) {
this[this.length] = value;
stolen = this;
});
var myObject = {
a: 2
};
----------------------------------------
var myObject = {};
Object.defineProperty( myObject, "a", {
value: 2,
writable: true,
configurable: true,
enumerable: true
} );
defineProperty
var myObject = {};
Object.defineProperty( myObject, "a", {
value: 2,
writable: false
} );
myObject.a = 22;
console.log( myObject.a );
Object.defineProperty( myObject, "a", {
value: 333,
} );
console.log( myObject.a );
defineProperty
Watch out for configurable!
defineProperty
Object.preventExtensions
Object.seal
Object.freeze
What's the difference?
Object.defineProperty( myObject, "b", {
get: function(){ return this.a * 2 },
set: function(a){ this.a = a - 1},
configurable: true,
enumerable: true
}
);
var myObject = {
get b() { return this.a * 2 },
set b(a) { this.a = a - 1}
}
What happens, if we leave one out?
function fn(x,y,z) {
console.log( x + y + z );
}
fn( ...[1,2,3] );
var a = [2,3,4];
var b = [ 1, ...a, 5 ];
console.log( b );
function foo(x, y, ...z) {
console.log( x, y, z );
}
foo( 1, 2, 3, 4, 5 );
function foo(...args) {
console.log( args );
}
foo( 1, 2, 3, 4, 5);
let x = {
a: 1,
b: 2,
}
let y = {
...x,
b: 4,
c: 5,
}
console.log( y );
var w1 = new Worker( "http://some.url.1/mycoolworker.js" );
w1.addEventListener( "message", function(evt){
// evt.data
} );
w1.postMessage( "something cool to say" );
-----------------------------------------------------------
addEventListener( "message", function(evt){
// evt.data
} );
postMessage( "a really cool reply" );
Sharing data
By Benedek Gagyi