1. 程式人生 > >Javascript function prototype and object prototype

Javascript function prototype and object prototype

Object.setPrototypeOf()

Object.setPrototypeOf(object, prototype) could change the prototype of the object directly, but only IE 10 and below do not support the method

Function.prototype

Every function has a prototype property, which could be set with assignment. We could achieve similar effect as Object.setPrototypeOf()

with new.

function func() {
}
function createPrototype(prototype) {
    for (var key in prototype) {
        this[key] = prototype[key];
    }
}
func.prototype = new createPrototype({ key: value });