Lodash - create 方法

语法

_.create(prototype, [properties])

创建一个从原型对象继承的对象。如果给出了属性对象,则将其自己的可枚举字符串键属性分配给创建的对象。

参数

  • prototype (Object) − 要继承的对象。

  • [properties] (Object) − 要分配给对象的属性。

输出

  • (Object) −返回新对象。

示例

var _ = require('lodash');
function Shape() {
    this.x = 0;
    this.y = 0;
}
function Circle() {
Shape.call(this);
}
Circle.prototype = _.create(Shape.prototype, {
    'constructor': Circle
});

var circle = new Circle;

console.log(circle instanceof Circle);
console.log(circle instanceof Shape);

将上述程序保存在 tester.js 中。运行以下命令执行此程序。

命令

\>node tester.js

输出

true
true

lodash_object.html