'inheritance'에 해당되는 글 2건

  1. 2008.02.20 javascript object, class & inheritance, sigletons
  2. 2008.02.20 objects, collections, linkage, invocations, this, typeof, closure, namespace, inheritance

javascript object, class & inheritance, sigletons

ITWeb/개발일반 2008.02.20 17:08

 
Class & Inheritance

사용자 삽입 이미지



















 
Class & Inheritance
-Prototypal Inheritance
var oldObject = {
    firstMethod: function () { alert("first"); },
    secondMethod: function () { alert("second"); }
};
var newObject = new Object(oldObject);
newObject.thirdMethod = function () { alert("third"); };
var myDoppelganger = new Object(newObject);
myDoppelganger.firstMethod(); // or
myDoppelganer[“firstMethod”]();
var obj1 = function () {
this.title = "obj1";
}
var obj2 = function () {}
obj2.prototype = new obj1;
var obj2Class = new obj2();
alert(obj2Class.title);
obj1 = {
title:"obj1_"
}
obj2 = new Object(obj1);
alert(obj2.title);

 
-Method apply (Member variable)
§Function.apply(thisArg[, argArray])
function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
 
function RentalCar(carNo, make, model, year) {
  this.carNo = carNo;
  Car.apply(this, new Array(make, model, year));
}
 
myCar = new RentalCar(2134,"Ford","Mustang",1998);
document.write("Your car is a " + myCar.year + " " + myCar.make + " " + myCar.model + ".");

 
-Method apply (Member function)
§function TopClass (name, value) {
§ this.name = name;
§ this.value = value;
§ this.getAlertData = function () {
§ alert(this.name);
§ alert(this.value);
§ }
§}
§function SubClass (name, value, dept) {
§ this.dept = dept;
§ TopClass.apply(this, arguments);
§ this.getView = function () {
§ this.getAlertData();
§ }
§}
§//SubClass.prototype = new TopClass();
§var objSubClass = new SubClass("상위클래스","super","community");
§objSubClass.getView();

 
-Method call
§Function.call(thisArg[, arg1[, arg2[, ...]]])
function car(make, model, year) {
    this.make = make, this.model = model, this.year = year;
}
 
function hireCar(carNo, make, model, year) {
    this.carNo = carNo, car.call(this, make, model, year);
}

 
-Prototype
Profiler = function () {..}
Profiler.prototype.name = function () {..}
-Namespace
YAHOO = {};
-new operator
function object(o) {
function F() {};
F.prototype = o;
return new F();
}
classA = new Profiler(); or classA = object(Profiler);

 
-Encapsulate (Parasitic)
YAHOO = {};
YAHOO.Trivia = function () {
    var privateVar = "showPoser";
    function privateNextPoser() {
        alert("getNextPoser");
    }
    return {
        getNextPoser: function (cat, diff) {
            privateNextPoser();
        },
        showPoser: function () {
            alert(privateVar);
        }
    };
} ();
function getFunc() {
var oObj = YAHOO.Trivia;
oObj.getNextPoser();
}
YAHOO = {};
YAHOO.Trivia = function () {
    var privateVar = "showPoser";
    function privateNextPoser() {
        alert("getNextPoser");
    }
    return {
        getNextPoser: function (cat, diff) {
            privateNextPoser();
        },
        showPoser: function () {
            alert(privateVar);
        }
    };
};
function getFunc() {
var oObj = new YAHOO.Trivia();
oObj.getNextPoser();
}

 
-Singletons
var singleton = function () {
    var privateVariable;
    function privateFunction(x) {
        ...privateVariable...
    }
    return {
        firstMethod: function (a, b) {
            ...privateVariable...
        },
        secondMethod: function (c) {
            ...privateFunction()...
        }
    };
}();





신고
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 : Comment 0

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().
 


신고
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 : Comment 0

티스토리 툴바