joshua님의 블로그 내용 중 저에게 필요한 내용만 골라서 정리한 글입니다. 자세한 내용은 joshua님의 블로그를 참조하시길 바랍니다.
function Parent() {}
function Child () {}
var some_var = 1;
var module1 = {}
var module2 = {}
---
var MYAPP = {};
MYAPP.Parent = function() {}
MYAPP.Child = function() {}
MYAPP.some_ver = 1;
MYAPP.modules = {}
MYAPP.modules.module1 = {}
MYAPP.modules.module2 = {}
pros
cons
var MYAPP = {}
if(typeof MYAPP == "undefined") {
var MYAPP = {}
}
or
var MYAPP = MYAPP || {}
var MYAPP = MYAPP || {}
MYAPP.namespace = function(ns_string) {
var parts = ns_string.split('.'),
parent = MYAPP,
i;
if(parts[0] === "MYAPP"){
parts = parts.slice(1);
}
for (i = 0; i < parts.length; i += 1) {
if(typeof parent[parts[i]] === "undefine") {
parent[parts[i]] = {}
};
parent = parent[part[i]]
}
return parent;
}
위 코드를 아래처럼 사용할 수 있다.
MYAPP.namespace("MYAPP.modules.module2);
위 코드는 아래와 같다
var MYAPP = {
module : {
moduels : {}
}
}
var module2 = MYAPP.namespace("MYAPP.moduels.moduels2");
module2 === MYAPP.modules.moduels2 // true
"MYAPP.moduels.moduels2"
의 내용을 객체로 만들어주는 과정var myFunction = function() {
var event = YAHOO.util.EVENT,
dom = YAHOO.util.Dom;
}
pros
function test1() {
alert(MYAPP.modules.m1)
alert(MYAPP.modules.m2)
}
function test2() {
var modules = MYAPP.modules;
alert(moduels.m1)
alert(moduels.m2)
}
function Gadget() {
var name = "iPhone";
this.getName = function() {return name;}
}
var toy = new Gadget();
console.log(toy.name)
console.log(toy.getName())
// 2. 객체 리터럴 이용
var myobj;
(function () {
var name = "Android"
myobj = {
getName: function() {
return name;
}
}
}())
console.log(myobj.getName())
var myobj = (function() {
var name = "Android"
return {
getName: function() {
return name;
}
}
}())
console.log(myobj.getName());
function Gadget() {
var name = "iPhone";
this.getName = function() {return name;}
}
Gadget.prototype = (function () {
var browser = "Mobile"
return {
getBrowser : function() {
return browser;
}
}
}())
var toy = new Gadget();
console.log(toy.getName()) // 객체 인스턴스의 비공개 멤버
console.log(toy.getBrowser()) // 프로토타입의 비공개 멤버
MYAPP.namespace("MYAPP.utilities.array")
MYAPP.utilities.array = (function() {
return {
inArray: function (e) {
//...
},
isArray: function (e) {
//...
}
}
}());
MYAPP.utilities.array = (function() {
// 의존 관계
var uobj = MYAPP.utilities.object,
ulang = MYAPP.utilities.lang,
// 비공개 프로퍼티
array_string = "[object Array]",
ops = Object.prototype.toString;
// 공개 API
return {
inArray: function (e) {
//...
},
isArray: function (e) {
return ops.call(e) === array_string
}
}
}());
MYAPP.utilities.array = (function() {
// 의존 관계
var uobj = MYAPP.utilities.object,
ulang = MYAPP.utilities.lang,
// 비공개 프로퍼티
array_string = "[object Array]",
ops = Object.prototype.toString;
// 비공개 API
inArray = function(e) {
//..
}
isArray = function(e) {
return ops.call(e) === array_string
}
// 공개 API
return {
isArray: isArray
}
}());
var MYAPP = {};
MYAPP.Parent = function() {}
MYAPP.Child = function() {}
MYAPP.modules = {}
MYAPP.modules.module1 = {}
MYAPP.modules.module2 = {}
var myFunction = function() {
var event = YAHOO.util.EVENT,
dom = YAHOO.util.Dom;
}
var myobj = (function() {
var name = "Android"
return {
getName: function() {
return name;
}
}
}())
MYAPP.utilities.array = (function() {
// 비공개 프로퍼티
var array_string = "[object Array]",
ops = Object.prototype.toString;
// 비공개 API
inArray = function(e) {
}
isArray = function(e) {
return ops.call(e) === array_string
}
// 공개 API
return {
isArray: isArray,
inArray: function (e) {
},
isArray: function (e) {
return ops.call(e) === array_string
}
}
}
}());
Javascript object creation patterns tutorial 또한 참고
var obj = {
myprop : "my value"
}
var obj2 = {
myprop : "my value"
}
obj === obj2 // false
obj == obj2 // false
var peopleFactory = function(name, age, state) {
var temp = {};
temp.age = age;
temp.name = name;
temp.state = state;
temp.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.state)
}
return temp;
}
var person1 = peopleFactory('john',23,'CA')
var person2 = peopleFactory('kassie',27,'KT')
person1.printPerson()
person2.printPerson()
var peopleConstructor = function(name, age, state){
this.name = name
this.age = age
this.state = state
this.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.state)
}
}
var person1 = new peopleConstructor('john',23,'CA')
var person2 = new peopleConstructor('kassie',27,'KT')
person1.printPerson();
person2.printPerson();
var peopleProto = function() {}
peopleProto.prototype.age = 0;
peopleProto.prototype.name = "no name";
peopleProto.prototype.city = "no city";
peopleProto.prototype.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.city )
}
var person1 = new peopleProto();
person1.name = "john"
person1.age = 23
person1.city = "CA"
person1.printPerson();
var person2 = new peopleProto();
person2.printPerson()
console.log('name' in person1)
console.log(person1.hasOwnProperty('name'))
cons
Pizza.getCrust = function() {
return this.crust
}
Pizza.prototype.getCrust = function() {
return this.crust
}
var peopleDynamicProto = function(name, age, state) {
this.age = age;
this.name = name
this.city = city
// 만약 지금 printPerson 함수가 없다면
if(typeof this.printPerson !== 'funciton') {
// prototype에 printPerson 함수를 생성해라
peopleDynamicProto.prototype.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.city )
}
}
}
var person1 = new peopleDynamicProto("john",23,"CA")
person1.printPerson()
function Fedex() {
this.calculate = package => {
return 1;
}
function UPS() {
this.calculate = package => {
return 2;
}
const fedex = new Fedex();
const ups = new UPS();
function Shipping() {
this.company
this.setStrategy = company => {
this.company = company;
}
this.calculate = package => {
return this.company.calculate(package)
}
}
const shipping = new Shipping();
var package = { from ; 'SF', to : 'KT'}
shipping.setStrategy(fedex)
shipping.calculate(pacakge)
var obj = {
myprop : "my value"
}
var peopleFactory = function(name, age, state) {
var temp = {};
temp.age = age;
temp.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.state)
}
return temp;
}
var person1 = peopleFactory('john',23,'CA')
person1.printPerson()
var peopleConstructor = function(name, age, state){
this.name = name
this.age = age
this.state = state
this.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.state)
}
}
var person1 = new peopleConstructor('john',23,'CA')
person1.printPerson();
var peopleProto = function() {}
peopleProto.prototype.age = 0;
peopleProto.prototype.name = "no name";
peopleProto.prototype.city = "no city";
peopleProto.prototype.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.city )
}
var person1 = new peopleProto();
person1.name = "john"
person1.age = 23
person1.city = "CA"
person1.printPerson();
var peopleDynamicProto = function(name, age, state) {
this.age = age;
this.name = name
this.city = city
if(typeof this.printPerson !== 'funciton') {
peopleDynamicProto.prototype.printPerson = function() {
console.log(this.name + ", " + this.age + ", " + this.city)
}
}
}
var person1 = new peopleDynamicProto("john",23,"CA")
person1.printPerson()
unction SignalFire(id, logs) {
this.id = id;
this.logs = logs;
this.functionality1: function() {
},
this.functionality2: function() {
}
}
SignalFire.prototype = {
functionality1: function() {
},
functionality2: function() {
},
}
var a = 1,
b = "hello",
c = ["a","b","c"];
ar nameSpace = {
a : "1",
b : 23,
c : function() {
// ...
}
};
onClick=nameSpace.c();
Debugging Javascript - Beginner to Advanced in One Video
50 Dev tool tips and tricks: Become an expert front end developer