Angular 1.4.8/JS:创建构造函数并将属性继承到第三个对象中
Angular 1.4.8 / JS : create constructor and inherit properties into a 3rd object
DEV TOOLS
Angular 1.4.8&lodash
问题
编辑
澄清我的问题:
- 创建对象(文章)
- 应用构造函数
- 然后导入第三个对象的属性,但将其放置在proto文件夹,这样我就不会得到大量属性在根中
正如我所说,我的代码是有效的,但我问,因为我来自一个更结构化的语言背景,继承只是感觉脆弱和混乱,但也许这就是JS ES5。。。6看起来更好。
下面的代码是有效的,但由于我是JS的新手,我想知道是否有更好的方法来构建一个对象("类"),在这个对象中,我从传入的数据/键创建一个consctutor,然后在proto文件夹的节/文件夹中用第三个对象的属性扩展我的当前对象?构建构造并继承属性的Extendable()
函数感觉很难看。
在以下示例中:
- 我通过注入数据和密钥来创建一个文章实例。然后我使用constructor工厂为创建构造函数CCD_ 2
- 然后我用3rd的属性扩展文章实例对象,模型(模型提供程序),但也希望将它们放在具有特定键"Eloquent"的
__proto__
文件夹中
示例:
Extendable
// Extendable constructor properties
__proto__: Extendable
Eloquent: Object
// 'Eloquent model' inherited properties
代码:
(function() {
'use strict';
_Constructor.$inject = [];
function _Constructor() {
function __constructor(data, keys) {
_.assign(this, _.pick(data, keys));
}
return __constructor;
}
ArticleModel.$inject = ['model', '_Constructor'];
function ArticleModel(model, _Constructor) {
function Extendable(data, keys) {
_Constructor.call(this, data, keys);
// This works but NOT pretty. Is there a cleaner or better way to decorate / inherit e.g. is there a ::__parent() method?
var decorate = Extendable.prototype['Eloquent'] = {};
// model is a the main model provider
_.assign(decorate, model);
}
Extendable.prototype = Object.create(_Constructor.prototype);
Extendable.prototype.constructor = Extendable;
var article = Extendable.prototype;
article.pleaseWork = function() {
return 'The JS gods are pleased! move forward 1 test!';
};
return Extendable;
}
angular.module('article', [
])
.factory('_Constructor', _Constructor)
.service('article', ArticleModel)
})();
我不完全理解您在这里想要实现的目标。听起来您正在尝试创建新的对象,这些对象也将继承基本对象的属性,也可能继承第三个对象的属性。
假设您有一个基对象:base={foo:1}现在您有了另一个对象:third={third:1}现在,我们想要创建一个对象,它从基开始抑制,并且也从第三个开始具有属性。
var newGuyOnTheBlock = Object.create(base); //creates a new object from base and will have all properties from base
_.extend(newGuyOnTheBlock, third); // now it'll have properties from third and base
相关文章:
- 可点击的三个JS凸对象(点击一次即可显示图像)
- 比较对象类型时是否需要第三个=
- JS 字段验证 - 循环遍历对象,每个字段有三个数据注意事项
- 在三个.js中加载多个 Blender 导出的对象/网格 + 材质
- 三个js,鼠标点击时弹跳对象
- Angular JS将2个对象合并为第三个对象
- 将外部纹理添加到三个.js场景对象
- es6ify.runtime 使我的三个 js 导入空对象
- 可点击的三个 Js 对象/凸项
- 两个 html 对象(列表、输入)应该生成第三个列表
- 如何告诉三个Javascript对象所有内容都已加载
- 无法渲染在 jQuery $.get 请求函数中添加到场景中的三个 js 对象
- 将对象从三个.js导出到任何内容
- 三个 js 对象在同一个地方相交 - 如何分辨要显示哪一个
- 如何在三个.js中加载一个 json 对象数组
- STL 对象的横截面使用三个.js
- 三个 JS - 如何缩放移动对象
- Angular 1.4.8/JS:创建构造函数并将属性继承到第三个对象中
- Lodash处理两个对象,但如果处理三个对象,则返回null
- 将旋转从父对象Object3D转移到子对象-三个js