Wednesday, December 21, 2016

javascript inheritance: Two - (2) test 5

@reference_-1_overall
@reference_-2_inheritance-Two (1)

//test start - 5
//using default: 'Employee.prototype = Object.create(Person.prototype)'

try {
    //Person.prototype.sayHello(); // output: Hello, my name is undefined.
    //Person.prototype.run(); // output: undefined is running.
    //Person.prototype.walk(); //throw an error: Object doesn't support property or method 'walk'
    //
    //Employee.prototype.sayHello(); // output: Hello, my name is undefined, I'm the undefined
    //Employee.prototype.run(); // output: undefined is running.
    //Employee.prototype.walk(); //throw an error: Object doesn't support property or method 'walk'

    let jack = Object.create(Employee.prototype, {
        name: {
            value: "Jack",
            writable: true,
            enumerable: true,
            configurable: true
        },
        title: {
            value: "Manager",
            writable: true,
            enumerable: true,
            configurable: true
        }
    }
);
    //jack.sayHello(); // output: Hello, my name is Jack, I'm the Manager
    //alert(Employee.prototype.isPrototypeOf(jack)); // output: true
    //jack.run(); // output: Jack is running.
    //jack.walk(); // throw an error: Object doesn't support property or method 'walk'
    //Employee.prototype.sayHello.call(jane); // output: Hello, my name is Jane, I'm the Accountant.
    //Person.prototype.sayHello.call(jane); // output: Hello, my name is Jane.


    //var jane = new Employee("Jane", "Accountant");
    let y = {};
    y.prototype = jane;
    //y.sayHello(); // throw an error: Object doesn't support property or method 'sayHello'
    //y.run(); // throw an error: Object doesn't support property or method 'run'
    //y.walk(); // throw an error: Object doesn't support property or method 'walk'

    let z = function() {};
    z.prototype = Object.create(jane);
    let zi = new z();
    zi.sayHello(); // output: Hello, my name is Jane, I'm the Accountant.
    zi.run(); // output: Jane is running.
    zi.walk(); // output: Jane is walking.
    //alert(jane.isPrototypeOf(zi)); // output: true             

    let a = function() {};
    a.prototype = Object.create(null);

    //alert(z.prototype === a.prototype); // output: false
    //alert(compareObj(z.prototype, a.prototype)); // output: true  
    //alert(compareContent(z.prototype, a.prototype)); // throw an error: Object doesn't support property or method 'isPrototypeOf'

    //alert(z.prototype.isPrototypeOf(a.prototype)); // output: false
    //alert(a.prototype.isPrototypeOf(z.prototype)); // throw an error: Object doesn't support property or method 'isPrototypeOf'

    //Note that compareObj & compareContent function may change

    //alert(deepCompare(z.prototype, a.prototype));  // throw an error: Number Expected
    //alert(z.prototype.constructor); // the output is the Employee's constructor
    //alert(a.prototype.constructor); // output: undefined
    //alert(z === a); // output: false
    //alert(compareObj(z, a)); // output: true
    //alert(compareContent(z, a)); // output: true
    //alert(deepCompare(z, a)); // output: true     
           
    //test end - 5

 
//test start - 6
let b = Object.create(jane);
//alert(compareObj(b, jane)); // output: false
//alert(Object.keys(b).length + " -- " + Object.keys(jane).length); // output: 0 -- 3

//txt = outputObj(b);
//txt += "===========================<br>";
//txt += outputObj(jane); // output: the object output are the same

c = function(name) {
    this.name = name;
    this.constructor.prototype.sayHello = function() {
        alert("Hello, " + this.name);
    };
    alert("hello");
};
function d(name) {
    this.name = name;
    d.prototype.sayHello = function() {
        alert("Hello, " + this.name);
    };
    alert("hello");
};
c.prototype = {a: 1};
d.prototype = {b: 1};
let g = new c("Jason"); // output: hello
let h = new d("Jason"); // output: hello
//g.sayHello(); // output: Hello, Jason
//c.prototype.sayHello(); // output: Hello, undefined
//h.sayHello(); // output: Hello, Jason
//d.prototype.sayHello(); // output: Hello, undefined

function i(name) {
    this.name = name;
    this.sayHello = function() {
        alert("Hello, " + this.name);
    };
    alert("hello");
};
//i.prototype.sayHello();//throw an error: Object doesn't support property or method 'sayHello'
j = function(name) {
    this.name = name;
    this.constructor.prototype.sayHello = function() {
        alert("Hello, " + this.name);
    };
    alert("hello");
};
//let k = new j("Jason"); // output: hello
//alert(g.constructor === k.constructor); // output: false
//let l = new j("Jason"); // output: hello
//alert(k.constructor === l.constructor); // output: true
//alert(compareObj({}.constructor, [].constructor)); // output: false
//alert(compareContent({}.constructor, [].constructor)); // output: false
//alert(deepCompare({}.constructor, [].constructor)); // output: false            


let e = {a: 1, b: 2, c: 3},
     f = {a: 1, b: 2, c: 3};
//alert(compareContent(e, f));// output: true
//alert(compareObj(e, f)); // output: true
//alert(deepCompare(e, f)); // output: true

e.prototype = {d: 4};
f.prototype = {d: 4};
//alert(compareContent(e, f));// output: false
//alert(compareObj(e, f)); // output: true
//alert(deepCompare(e, f)); // output: false

}catch (err) {
    alert(err.message);
}
//test end - 6

No comments:

Post a Comment