objects, collections, linkage, invocations, this, typeof, closure, namespace, inheritance
ITWeb/개발일반 2008. 2. 20. 16:36Objects
–-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. 
–
§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().
•







