'namespace'에 해당되는 글 2건

  1. 2008.02.20 objects, collections, linkage, invocations, this, typeof, closure, namespace, inheritance
  2. 2007.04.03 [강좌] namespace (1)

objects, collections, linkage, invocations, this, typeof, closure, namespace, inheritance

ITWeb/개발일반 2008.02.20 16:36

Objects
-Everything else is objects
-Objects can contain data and methods
-Objects can inherit from other objects.

 
Collections
-An object is an unordered collection of name/value pairs
-Names are strings
-Values are any type, including other objects
-Good for representing records and trees
-Every object is a little database
§var FUNC = function () {
§ var objDiv = document.createElement("div");
§ this.getCollection = function ( collection ) {
§ for ( i in collection ) {
§ objDiv.innerHTML += "name : " + i + "<br>value : " + collection[i] + "<br>";
§ }
§ document.body.appendChild(objDiv);
§ }
§}
§FUNC = new FUNC();

 
Object Literals
-Object literals are wrapped in { }
-Names can be names or strings
-Values can be expressions
-: separates names and values
-, separates pairs
-Object literals can be used anywhere a value can appear

 
Linkage
-Objects can be created with a secret link to another object.
-If an attempt to access a name fails, the secret linked object will be used.
-The secret link is not used when storing. New members are only added to the primary object.
-The object(o) function makes a new empty object with a link to object o.

function object(o) {
function F() {};
F.prototype = o;
return new F();
}

 
JavaScript Invocations
-If a function is called with too many arguments, the extra arguments are ignored.
-If a function is called with too few arguments, the missing values will be undefined.
-There is no implicit type checking on the arguments.
-There are four ways to call a function:
§Function form
functionObject(arguments)
§Method form
thisObject.methodName(arguments)
thisObject["methodName"](arguments)
§Constructor form
new functionObject(arguments)
§Apply form
functionObject.apply(thisObject, [arguments])

 
JavaScript this
-this is an extra parameter. Its value depends on the calling form.
-this gives methods access to their objects.
-this is bound at invocation time.
Invocation form
this
function
the global object
method
the object
constructor
the new object

 
JavaScript typeof
type
typeof
object
'object'
function
'function'
array
'object'
number
'number'
string
'string'
boolean
'boolean'
null
'object'
undefined
'undefined'

 
Closure
-The scope that an inner function enjoys continues even after the parent functions have returned.
-This is called closure.
§function getClosure () {
§  var num = 1;
§  var getAlert = function() { num++; alert(num); }
§  num++;
§  return getAlert();
§}
§<input type="button" value="getClosure" onclick="getClosure();">

 
function fade(id) {
    var dom = document.getElementById(id),
        level = 1;
    function step () {
        var h = level.toString(16);
        dom.style.backgroundColor =
            '#FFFF' + h + h;
        if (level < 15) {
            level += 1;
            setTimeout(step, 100);
        }
    }
    setTimeout(step, 100);
}

 
JavaScript Namespace
-Every object is a separate namespace.
-Use an object to organize your variables and functions.
-The YAHOO Object.
§var YAHOO = {};
§YAHOO.url = "http://www.yahoo.com";
§YAHOO.getUrl = function () {
§ alert(this.url);
§ return this.url;
§}
§<input type="button" value="YAHOO.getUrl();" onclick="YAHOO.getUrl();">

 
Inheritance
-Linkage provides simple inheritance.
-Prototypal inheritance.
-Parasitic inheritance.
-Method apply(), call().
 


신고
Trackback 0 : Comment 0

[강좌] namespace

ITWeb/개발일반 2007.04.03 18:38

ajax 가 보편화 되면서 javascript 에 대한 oop 개발 방법이 많이 도입되고 있는데요.
그냥 공부도 할겸 client application 에 관심이 많은 지라 javascript 강좌를 개설 했습니다.

뭐 우선 내가 좋아라 하는 거나 만들고 있는 것 부터 시작을 할까 합니다.
그 첫번째가 namespace 인데요.

http://www.mozilla.org/js/language/js20/core/namespaces.html

If a namespace is defined as a member of a class, then the namespace must be declared static.
번역을 하자면.. (참고로 저는 영어 그닥 잘하지 못합니다.. ^^;)
클래스 맴버로 namespace 가 선언되어 있으면 선언된 namespace 는 반듯이 static 으로 선언 되어 진다.
라는 이야기 입니다.

예를 통해서 확인해 보죠..

http://www.junetool.com/splv/namespace.html

- web.js 코드
/*
   namespace 를 등록하는 이유는 javascript 의 OOP 개발 방법을 적용하기 위한 방법중 하나이다.
*/
// global class (package)
var web = web || {};

// name space function
web.namespace = function ( sNS ) {
    var aNS = sNS.split(".");
    var oTopClass = web;
    var i=0;

    for ( i=0; i<aNS.length; i++ ) {
        oTopClass[aNS[i]] = oTopClass[aNS[i]] || {};
    }
}

-- namespace.html 코드
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>javascript::namespace</title>
</head>
<script src="web.js"></script>
<script>
web.util = {
    temp:"web.util"
}

web.util.string = {
    temp:"web.util.string"
}
</script>

<body>

<script>
web.namespace("WEB.util.string");
alert(web.util.temp + "\n" + web.util.string.temp);
</script>
</body>
</html>

요기까지 허접 설명 이였구요.
좀 다듬어진걸로 해서 서버에 올리고 공유 하지요.

참 요즘 표준화에도 관심이 많습니다.
표준화에 대해서도 시간이 되는 데로 욜심히 올려 보도록 하겠습니다.

신고
Trackback 0 : Comment 1