Originally posted by d000hg
View Post
var foo = {};
foo.bar = 1;
foo.baz = 2;
var blob = {};
blob.bar = [];
blob.qaz = 'a';
var x = {};
x.bar = foo;
// assume hidden class A represents an object with nothing but default properties foo becomes an instance of existing hidden class A; foo has property "bar" added: it becomes an instance of new hidden class B, which descends from A; foo has property "baz" added: it becomes an instance of new hidden class C, which descends from B; blob becomes an instance of existing hidden class A; blob has property "bar" added: it becomes an instance of existing hidden class B; blob has property "qaz" added: it becomes an instance of new hidden class D, which descends from B; x becomes an instance of existing hidden class A; x has property "bar" added: it becomes an instance of existing hidden class B; // so at this point: // foo is an instance of C // blob is an instance of D // x is an instance of B delete blob.qaz; // blob can now go back to being an instance of B
typeA = GetType(a)
typeB = GetType(b)
if(typeA == int AND typeB==int)
{
integerAdd (a,b)
}
else if (typeA == string AND typeB == string)
{
stringAdd(a,b)
}
else if(typeA == string AND typeB == int)
{
AddIntToString(a,b)
}
else ....
function concatenateStrings(first, second) {
// allocate new buffer having length first.length + second.length
// copy first into new buffer
// copy second into new buffer at offset first.length
// return new buffer as type string
}
function concatenateStringAndNumber(string, number) {
return concatenateStrings(string, number.toString(10));
}
function addNumberAndString(number, string) {
return addStringAndNumber(string, number);
}
function addNumberAndNumber(number1, number2) {
// return the sum of the two numbers
}
addFunctions = {
string: {
string: concatenateStrings,
number: concatenateStringAndNumber
},
number: {
string: addNumberAndString,
number: addNumberAndNumber
}
};
function whatWeDoWhenAdding(value1, value2) {
addFunctions[typeof value1][typeof value2](value1, value2);
}
typeA = GetType(a)
typeB = GetType(b)
if(typeA == int AND typeB==int)
{
integerAdd (a,b)
}
else if (typeA == string AND typeB == string)
{
stringAdd(a,b)
}
else if(typeA == string AND typeB == int)
{
AddIntToString(a,b)
}
else ....


Leave a comment: