2014-10-09 05:47:38 -04:00
|
|
|
# Functions
|
|
|
|
|
2014-08-20 15:05:28 -04:00
|
|
|
# prototype
|
2011-06-20 13:40:28 -04:00
|
|
|
snippet proto
|
2014-09-18 10:59:42 -04:00
|
|
|
${1:class_name}.prototype.${2:method_name} = function(${3}) {
|
2014-09-12 11:56:36 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
};
|
|
|
|
# Function
|
|
|
|
snippet fun
|
2014-09-18 10:59:42 -04:00
|
|
|
function ${1:function_name}(${2}) {
|
2014-09-12 11:56:36 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
|
|
|
# Anonymous Function
|
|
|
|
snippet f
|
2014-08-20 15:05:28 -04:00
|
|
|
function(${1}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2014-06-18 12:14:51 -04:00
|
|
|
}
|
2014-09-12 12:12:00 -04:00
|
|
|
# Function assigned to variable
|
|
|
|
snippet vf
|
2014-09-16 04:24:10 -04:00
|
|
|
var ${1:function_name} = function $1(${2}) {
|
2014-09-12 12:12:00 -04:00
|
|
|
${0}
|
2014-09-15 07:52:04 -04:00
|
|
|
};
|
2011-06-20 13:40:28 -04:00
|
|
|
# Immediate function
|
|
|
|
snippet (f
|
2014-08-20 15:05:28 -04:00
|
|
|
(function(${1}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}(${2}));
|
2014-10-09 05:47:38 -04:00
|
|
|
# self-defining function
|
|
|
|
snippet sdf
|
|
|
|
var ${1:function_name} = function (${2:argument}) {
|
|
|
|
${3}
|
|
|
|
|
|
|
|
$1 = function ($2) {
|
|
|
|
${0}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# Flow control
|
|
|
|
|
2011-06-20 13:40:28 -04:00
|
|
|
# if
|
|
|
|
snippet if
|
|
|
|
if (${1:true}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
|
|
|
# if ... else
|
|
|
|
snippet ife
|
|
|
|
if (${1:true}) {
|
|
|
|
${2}
|
|
|
|
} else {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
|
|
|
# tertiary conditional
|
2012-09-30 06:53:44 -04:00
|
|
|
snippet ter
|
2014-10-09 05:47:38 -04:00
|
|
|
${1:/* condition */} ? ${2:/* if true */} : ${0:/* if false */}
|
2011-06-20 13:40:28 -04:00
|
|
|
# switch
|
|
|
|
snippet switch
|
2012-09-30 06:53:44 -04:00
|
|
|
switch (${1:expression}) {
|
2011-06-20 13:40:28 -04:00
|
|
|
case '${3:case}':
|
2013-06-20 17:33:44 -04:00
|
|
|
${4}
|
2011-06-20 13:40:28 -04:00
|
|
|
break;
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
default:
|
2013-06-20 17:33:44 -04:00
|
|
|
${2}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
|
|
|
# case
|
|
|
|
snippet case
|
|
|
|
case '${1:case}':
|
2013-06-20 17:33:44 -04:00
|
|
|
${2}
|
2011-06-20 13:40:28 -04:00
|
|
|
break;
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2014-10-09 05:47:38 -04:00
|
|
|
# try
|
|
|
|
snippet try
|
|
|
|
try {
|
|
|
|
${1}
|
|
|
|
} catch (${2:e}) {
|
|
|
|
${0:/* handle error */}
|
|
|
|
}
|
|
|
|
# return
|
|
|
|
snippet ret
|
|
|
|
return ${0:result};
|
|
|
|
|
|
|
|
# Loops
|
|
|
|
|
|
|
|
# for loop
|
2011-06-20 13:40:28 -04:00
|
|
|
snippet for
|
2013-03-05 23:15:11 -05:00
|
|
|
for (var ${2:i} = 0, l = ${1:arr}.length; $2 < l; $2 ++) {
|
2013-07-25 17:17:32 -04:00
|
|
|
var ${3:v} = $1[$2];${0:}
|
2012-01-30 10:20:46 -05:00
|
|
|
}
|
2014-10-09 05:47:38 -04:00
|
|
|
# Reversed for loop
|
2011-06-20 13:40:28 -04:00
|
|
|
snippet forr
|
2013-03-05 23:15:11 -05:00
|
|
|
for (var ${2:i} = ${1:arr}.length - 1; $2 >= 0; $2 --) {
|
2013-07-25 17:17:32 -04:00
|
|
|
var ${3:v} = $1[$2];${0:}
|
2012-01-30 10:20:46 -05:00
|
|
|
}
|
2014-10-09 05:47:38 -04:00
|
|
|
# While loop
|
2011-06-20 13:40:28 -04:00
|
|
|
snippet wh
|
|
|
|
while (${1:/* condition */}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
2014-10-09 05:47:38 -04:00
|
|
|
# Do while loop
|
2011-06-20 13:40:28 -04:00
|
|
|
snippet do
|
|
|
|
do {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
} while (${1:/* condition */});
|
2014-10-09 05:47:38 -04:00
|
|
|
# For in loop
|
|
|
|
snippet fori
|
|
|
|
for (var ${1:prop} in ${2:object}) {
|
|
|
|
${0:$2[$1]}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Objects
|
|
|
|
|
2011-06-20 13:40:28 -04:00
|
|
|
# Object Method
|
|
|
|
snippet :f
|
2012-05-31 11:23:21 -04:00
|
|
|
${1:method_name}: function (${2:attribute}) {
|
2015-02-11 16:15:14 -05:00
|
|
|
${3}
|
2015-02-12 10:25:51 -05:00
|
|
|
},
|
2011-08-03 00:53:21 -04:00
|
|
|
# hasOwnProperty
|
|
|
|
snippet has
|
2013-07-25 17:17:32 -04:00
|
|
|
hasOwnProperty(${0})
|
2011-08-03 00:53:21 -04:00
|
|
|
# singleton
|
|
|
|
snippet sing
|
2011-08-03 08:56:12 -04:00
|
|
|
function ${1:Singleton} (${2:argument}) {
|
|
|
|
// the cached instance
|
|
|
|
var instance;
|
2011-08-03 00:53:21 -04:00
|
|
|
|
2011-08-03 08:56:12 -04:00
|
|
|
// rewrite the constructor
|
|
|
|
$1 = function $1($2) {
|
|
|
|
return instance;
|
|
|
|
};
|
2013-06-20 17:39:31 -04:00
|
|
|
|
2011-08-03 08:56:12 -04:00
|
|
|
// carry over the prototype properties
|
|
|
|
$1.prototype = this;
|
2011-08-03 00:53:21 -04:00
|
|
|
|
2011-08-03 08:56:12 -04:00
|
|
|
// the instance
|
|
|
|
instance = new $1();
|
2011-08-03 00:53:21 -04:00
|
|
|
|
2011-08-03 08:56:12 -04:00
|
|
|
// reset the constructor pointer
|
|
|
|
instance.constructor = $1;
|
2011-08-03 00:53:21 -04:00
|
|
|
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-08-03 00:53:21 -04:00
|
|
|
|
2011-08-03 08:56:12 -04:00
|
|
|
return instance;
|
|
|
|
}
|
2013-06-20 17:39:31 -04:00
|
|
|
# Crockford's object function
|
2013-03-14 14:13:36 -04:00
|
|
|
snippet obj
|
2013-03-15 13:53:35 -04:00
|
|
|
function object(o) {
|
2013-03-14 14:13:36 -04:00
|
|
|
function F() {}
|
|
|
|
F.prototype = o;
|
|
|
|
return new F();
|
|
|
|
}
|
2013-05-13 11:19:05 -04:00
|
|
|
# Define multiple properties
|
|
|
|
snippet props
|
|
|
|
var ${1:my_object} = Object.defineProperties(
|
2013-06-20 17:39:31 -04:00
|
|
|
${2:new Object()},
|
2013-05-13 11:19:05 -04:00
|
|
|
{
|
|
|
|
${3:property} : {
|
|
|
|
get : function $1_$3_getter() {
|
|
|
|
// getter code
|
|
|
|
},
|
|
|
|
set : function $1_$3_setter(value) {
|
|
|
|
// setter code
|
|
|
|
},
|
|
|
|
value : ${4:value},
|
|
|
|
writeable : ${5:boolean},
|
|
|
|
enumerable : ${6:boolean},
|
2013-07-25 17:17:32 -04:00
|
|
|
configurable : ${0:boolean}
|
2013-05-13 11:19:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
# Define single property
|
|
|
|
snippet prop
|
2013-06-20 17:39:31 -04:00
|
|
|
Object.defineProperty(
|
2013-05-13 11:19:05 -04:00
|
|
|
${1:object},
|
2014-10-08 05:09:42 -04:00
|
|
|
'${2:property}',
|
2013-05-13 11:19:05 -04:00
|
|
|
{
|
|
|
|
get : function $1_$2_getter() {
|
|
|
|
// getter code
|
|
|
|
},
|
|
|
|
set : function $1_$2_setter(value) {
|
|
|
|
// setter code
|
|
|
|
},
|
|
|
|
value : ${3:value},
|
|
|
|
writeable : ${4:boolean},
|
|
|
|
enumerable : ${5:boolean},
|
2013-07-25 17:17:32 -04:00
|
|
|
configurable : ${0:boolean}
|
2013-05-13 11:19:05 -04:00
|
|
|
}
|
|
|
|
);
|
2014-10-09 05:47:38 -04:00
|
|
|
|
|
|
|
# Documentation
|
|
|
|
|
|
|
|
# docstring
|
|
|
|
snippet /**
|
|
|
|
/**
|
|
|
|
* ${0:description}
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
snippet @par
|
|
|
|
@param {${1:type}} ${2:name} ${0:description}
|
|
|
|
snippet @ret
|
|
|
|
@return {${1:type}} ${0:description}
|
|
|
|
|
|
|
|
# JSON
|
|
|
|
|
|
|
|
# JSON.parse
|
|
|
|
snippet jsonp
|
|
|
|
JSON.parse(${0:jstr});
|
|
|
|
# JSON.stringify
|
|
|
|
snippet jsons
|
|
|
|
JSON.stringify(${0:object});
|
|
|
|
|
|
|
|
# DOM selectors
|
|
|
|
|
|
|
|
# Get elements
|
|
|
|
snippet get
|
2014-10-13 08:41:52 -04:00
|
|
|
getElementsBy${1:TagName}('${0}')
|
2014-10-09 05:47:38 -04:00
|
|
|
# Get element
|
|
|
|
snippet gett
|
2014-10-13 08:41:52 -04:00
|
|
|
getElementBy${1:Id}('${0}')
|
2014-10-09 05:47:38 -04:00
|
|
|
# Elements by class
|
2014-10-08 05:47:31 -04:00
|
|
|
snippet by.
|
2014-10-13 08:47:00 -04:00
|
|
|
${1:document}.getElementsByClassName('${0:class}')
|
2014-10-09 05:47:38 -04:00
|
|
|
# Element by ID
|
2014-10-08 05:47:31 -04:00
|
|
|
snippet by#
|
2014-10-13 08:47:00 -04:00
|
|
|
${1:document}.getElementById('${0:element ID}')
|
2014-10-09 05:47:38 -04:00
|
|
|
# Query selector
|
2014-10-08 05:47:31 -04:00
|
|
|
snippet qs
|
2014-10-13 08:47:00 -04:00
|
|
|
${1:document}.querySelector('${0:CSS selector}')
|
2014-10-09 05:47:38 -04:00
|
|
|
# Query selector all
|
2014-10-08 05:47:31 -04:00
|
|
|
snippet qsa
|
2014-10-13 08:47:00 -04:00
|
|
|
${1:document}.querySelectorAll('${0:CSS selector}')
|
2014-10-09 05:47:38 -04:00
|
|
|
|
|
|
|
# Debugging
|
|
|
|
|
|
|
|
# console.log
|
|
|
|
snippet cl
|
|
|
|
console.log(${0});
|
|
|
|
# console.debug
|
|
|
|
snippet cd
|
|
|
|
console.debug(${0});
|
2014-10-10 11:50:51 -04:00
|
|
|
# console.trace
|
|
|
|
snippet ct
|
|
|
|
console.trace(${0:label});
|
|
|
|
# console.time
|
|
|
|
snippet ctime
|
|
|
|
console.time(${0:label});
|
|
|
|
# console.assert
|
|
|
|
snippet ca
|
|
|
|
console.assert(${1:expression}, ${0:obj});
|
|
|
|
# console.dir
|
|
|
|
snippet cdir
|
|
|
|
console.dir(${0:obj});
|
2014-10-09 05:47:38 -04:00
|
|
|
|
|
|
|
# Misc
|
|
|
|
|
|
|
|
# setTimeout function
|
|
|
|
snippet timeout
|
|
|
|
setTimeout(function () {${0}}${2}, ${1:10});
|