On Github defectus / js_tutor
Created by Ondrej Linek/ @defectus
function a(){} // anonymously function(){}
var a = function a(){}
var a = new Function a('')
a(param1,param2,param3...)
a.call(this, param1, param2, param3)
a.apply(this, [params])
var a = new Array();
var a = [];
var o = new Object();
var o = {};
1/0 === Infinity
-1/0 === -Infinity
NaN !== NaN
('string' instance String) === true
var obj = {} // prototype will Object
var obj = function(){} // prototype will be Function
var obj = new function(){} // prototype will be Objectexplain how to create one of "maps" and how to create constructors
for (var propName in object) {console.log(propName + '=' + object[propName])}
var a = {b:'b'}; Object.keys(a);
function Constr1(val1){ this.prop1=val1; this.print=function(){alert(this.prop1)} } function Constr2(val1,val2){ Constr1.call(this, val1); this.prop2=val2; this.print2=function(){alert(this.prop1 + '-' + this.prop2)} } Constr2.prototype=Object.create(Constr1.prototype); var obj = new Constr2('a', 'b'); obj.print(); obj.print2();
window === global // true
window.alert = function(){window.confirm('we\'ve got a problem!')};alert('hello world');
var scope = {var1:'val1'}; alert(scope.var1);
var scope = jQuery.extend({}, {var1:'val1'}); alert(scope.var1);
function a () {alert('a called')} a = (function(a){ return function(){ alert('about to call a'); a();}})(a); a();
element.attachEvent('event', handler);
element.addEventListener('event', handler);
element.dispatchEvent(event);
var event = document.createEvent('MouseEvent'); event.initMouseEvent(.....); element.dispatchEvent(event);
element.event(); e.g. element.click();
$("div").css({color:'blue'}).on('click', handleClick).append(whatever);that's because many (not all!) functions return jquery object allowing calling additional functions use it whenever possible - the most epxensive operation is to actually find something
$("p.replaceMe").load( "ajax/replace.html"); $("p.replaceMeOnlyPart").load("ajax/replace.html#replaceBit"); $("p.replaceMeWith").load("ajax/replaceWithParams",{what:'anything'}); // POST method used! $("p.replaceMe".load("ajax/replace", function(){alert('replaced')}));
$.get("callMe") // ping - requests data but ignores result $.get("getMe", function(data){alert("received:" + data)}) // calls and gets result $.get("getMeWithParams",{param1:"val1"}, function(data){alert("received:" + data)}) // calls with paramters and gets result
$.ajax( {url:"ajax", success:function(data){alert("recevived:" + data)}}, error:function(data){"error:" + data.statusText}); $.ajax({url:"ajax", type:"POST", success:function(data){alert("recevived:" + data)}}, error:function(data){"error:"+ data.statusText}) // use POST method $.ajax({url:"ajax", type: "POST", data: {param1:"value1"}, success:function(data){alert("recevived:" + data)}}, error:function(data){"error:" + data.statusText}) // use POST with params
$.get("ajax").done(function(data) { alert("done:" + data)}) // when this ajax resolves call this function
$.when($.ajax("getData1"), $.ajax("getData2")) .then(function(data1, data2) {alert("done:" + data1 + data2)}, function(fail1, fail2){alert("failure:" + fail1 + fail2)}) // when all ajax resolve call this functionin case of single parameter the parameter is encapsulated as deffered
$.when($.ajax("getData1")) .then(function(data1) { alert("done:" + data1)}, function(fail1){alert("failure:" + fail1)}) // when this ajax resolves call this function
var defo = $.Deferred(); defo.done (function(data){alert("done1:" + data)}) .done(function(data){alert("done2:" + data)}) $("body").on("click", function(){defo.resolve("val")})if you want to prevent others from messing with your deffered objects then return the underlying promise e.g. defo.promise()which disallows resolving, notifying etc.
a quick example of a plugin below
(function($) { // closure // Plugin starts here $.fn.hilight = function( options ) { var $this = this; debug(this); return this.each(function() { var markup = $this.html(); markup = "" + markup + "" $this.html( markup ); }); }; // Private function for debugging. function debug( $obj ) { if ( window.console && window.console.log ) { window.console.log( "selection count: " + $obj.size() ); } }; })(jQuery); // we call the closure straight away
var s = document.getElementById('toFade').style; s.opacity = 1; (function fade(){(s.opacity-=.1)<0?s.display="none":setTimeout(fade,40)})();
[0,1,2,3,4,5].filter(function(v){return v > 2}); // [3,4,5]
var times2 = function(v){return v * 2}; [0,2,4,8,16].map(times2); // [0, 4, 8, 16, 32]
var sum = function(p, c, i){console.log('p:['+p+'];c:['+c+'];i:['+i+']');return p + c}; [0,2,4,8,16].reduce(sum); // 30 var max = function(p, c, i){console.log('p:['+p+'];c:['+c+'];i:['+i+']');return c > p ? c : p}; [0,2,4,8,16].reduce(max); // 16 var grouper = function(p, c, i){console.log('p:['+p+'];c:['+c+'];i:['+i+']');p[c]= (p[c]||0) + 1;return p}; [0,2,4,8,16,8,4,2].reduce(grouper,{}); // 16