2011-06-20 13:40:28 -04:00
|
|
|
# Prototype
|
|
|
|
snippet proto
|
|
|
|
${1:class_name}.prototype.${2:method_name} =
|
|
|
|
function(${3:first_argument}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0:// body...}
|
2011-06-20 13:40:28 -04:00
|
|
|
};
|
|
|
|
# Function
|
|
|
|
snippet fun
|
2012-04-27 15:36:20 -04:00
|
|
|
function ${1:function_name}(${2:argument}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0:// body...}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
|
|
|
# Anonymous Function
|
|
|
|
snippet f
|
2012-05-31 11:23:21 -04:00
|
|
|
function (${1}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2014-06-18 12:14:51 -04:00
|
|
|
}
|
2011-06-20 13:40:28 -04:00
|
|
|
# Immediate function
|
|
|
|
snippet (f
|
2012-05-31 11:23:21 -04:00
|
|
|
(function (${1}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}(${2}));
|
|
|
|
# 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
|
2013-07-25 17:17:32 -04:00
|
|
|
${1:/* condition */} ? ${2:a} : ${0:b}
|
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}
|
2011-06-20 13:40:28 -04:00
|
|
|
# for (...) {...}
|
|
|
|
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
|
|
|
}
|
2011-06-20 13:40:28 -04:00
|
|
|
# for (...) {...} (Improved Native For-Loop)
|
|
|
|
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
|
|
|
}
|
2011-06-20 13:40:28 -04:00
|
|
|
# while (...) {...}
|
|
|
|
snippet wh
|
|
|
|
while (${1:/* condition */}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
|
|
|
# try
|
|
|
|
snippet try
|
|
|
|
try {
|
2013-06-20 17:33:44 -04:00
|
|
|
${1}
|
2012-05-31 11:23:21 -04:00
|
|
|
} catch (${2:e}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0:/* handle error */}
|
2011-06-20 13:40:28 -04:00
|
|
|
}
|
|
|
|
# do...while
|
|
|
|
snippet do
|
|
|
|
do {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
} while (${1:/* condition */});
|
|
|
|
# Object Method
|
|
|
|
snippet :f
|
2012-05-31 11:23:21 -04:00
|
|
|
${1:method_name}: function (${2:attribute}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-06-20 13:40:28 -04:00
|
|
|
}${3:,}
|
|
|
|
# setTimeout function
|
|
|
|
snippet timeout
|
2013-07-25 17:17:32 -04:00
|
|
|
setTimeout(function () {${0}}${2}, ${1:10});
|
2011-06-20 13:40:28 -04:00
|
|
|
# Get Elements
|
|
|
|
snippet get
|
2013-07-25 17:17:32 -04:00
|
|
|
getElementsBy${1:TagName}('${2}')
|
2011-06-20 13:40:28 -04:00
|
|
|
# Get Element
|
|
|
|
snippet gett
|
2013-07-25 17:17:32 -04:00
|
|
|
getElementBy${1:Id}('${2}')
|
2011-06-20 13:40:28 -04:00
|
|
|
# console.log (Firebug)
|
|
|
|
snippet cl
|
2013-07-25 17:17:32 -04:00
|
|
|
console.log(${0});
|
2011-08-03 00:53:21 -04:00
|
|
|
# return
|
|
|
|
snippet ret
|
2013-07-25 17:17:32 -04:00
|
|
|
return ${0:result}
|
2011-08-03 00:53:21 -04:00
|
|
|
# for (property in object ) { ... }
|
|
|
|
snippet fori
|
2011-08-03 08:56:12 -04:00
|
|
|
for (var ${1:prop} in ${2:Things}) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0:$2[$1]}
|
2012-01-30 10:20:46 -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
|
|
|
# docstring
|
|
|
|
snippet /**
|
2011-08-03 08:56:12 -04:00
|
|
|
/**
|
2013-07-25 17:17:32 -04:00
|
|
|
* ${0:description}
|
2011-08-03 08:56:12 -04:00
|
|
|
*
|
|
|
|
*/
|
2011-08-03 00:53:21 -04:00
|
|
|
snippet @par
|
2013-07-25 17:17:32 -04:00
|
|
|
@param {${1:type}} ${2:name} ${0:description}
|
2011-08-03 00:53:21 -04:00
|
|
|
snippet @ret
|
2013-07-25 17:17:32 -04:00
|
|
|
@return {${1:type}} ${0:description}
|
2011-08-03 00:53:21 -04:00
|
|
|
# JSON.parse
|
|
|
|
snippet jsonp
|
2013-07-25 17:17:32 -04:00
|
|
|
JSON.parse(${0:jstr});
|
2011-08-03 00:53:21 -04:00
|
|
|
# JSON.stringify
|
|
|
|
snippet jsons
|
2013-07-25 17:17:32 -04:00
|
|
|
JSON.stringify(${0:object});
|
2011-08-03 00:53:21 -04:00
|
|
|
# self-defining function
|
|
|
|
snippet sdf
|
2011-08-03 08:56:12 -04:00
|
|
|
var ${1:function_name} = function (${2:argument}) {
|
2013-06-20 17:33:44 -04:00
|
|
|
${3}
|
2011-08-03 00:53:21 -04:00
|
|
|
|
2011-08-03 08:56:12 -04:00
|
|
|
$1 = function ($2) {
|
2013-07-25 17:17:32 -04:00
|
|
|
${0}
|
2011-08-03 08:56:12 -04:00
|
|
|
};
|
2013-03-14 13:38:03 -04:00
|
|
|
};
|
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},
|
2013-06-20 17:39:31 -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
|
|
|
}
|
|
|
|
);
|