Skip to content

Instantly share code, notes, and snippets.

@phannmalinka
Last active June 1, 2017 13:26
Show Gist options
  • Save phannmalinka/83ed0173f5ae5bfd101651066dd611ac to your computer and use it in GitHub Desktop.
Save phannmalinka/83ed0173f5ae5bfd101651066dd611ac to your computer and use it in GitHub Desktop.
A code snippet to create a class using requirejs structure style. Thank @jonnyreeves for this great structure. https://gist.github.com/jonnyreeves/2474026#file-person-js-L111
"Class": {
"prefix": "class",
"body": [
"/*===================================",
"** Author: Malika",
"** Email: malika.phann@gmail.com",
"** Description:",
"**",
"**===================================",
"**/",
"",
"define(function(){",
"",
"\t\"use strict\";",
"",
"\tfunction ${0:Person}(${1:name}){",
"\t\tif(!(this instanceof ${0:Person})){",
"\t\t\t throw new TypeError(\"${0:Person} constructor cannot be called as a function.\")",
"\t\t}",
"",
"\t\t/* member properties */",
"\t\tthis.${1:name} = ${1:name};",
"",
"\t\t/* private properties */",
"\t\tthis._age = -1;",
"\t}",
"",
"\t/* static properties */",
"\t${0:Person}.${3:gender} = \"male\";",
"",
"\t/* static methods */",
"\t${0:Person}.create = function(${1:name}){",
"\t\tvar instance = new ${0:Person}(${1:name});",
"\t\treturn instance;",
"\t}",
"",
"\t/* private methods */",
"\tfunction getName(){",
"\t\treturn ${1:name};",
"\t};",
"",
"\t/* prototype */",
"\t${0:Person}.prototype = {",
"\t\tconstructor: ${0:Person},",
"",
"\t\t/* setter and getter */",
"\t\tsetGender: function(${3:gender}){",
"\t\t\tthis.${3:gender} = ${3:gender};",
"\t\t},",
"",
"\t\t/* print member properties */",
"\t\ttoString: function(){",
"\t\t\treturn \"name = \"+this.${1:name}+\", age = \"+this.${2:age}+\", gender = \"+${3:gender};",
"\t\t}",
"\t};",
"",
"\treturn ${0:Person};",
"});"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment