扩展$resource对象从工厂中移除原型
Extending $resource object removes prototype from Factory
这是我现在的代码:
// Factory
angular.factory('Student', function() {
var defaults = {
StudentId: null
};
var Student = function(params) {
angular.extend(this, angular.copy(defaults), params);
};
// Prototype function for Student
Student.prototype.hasStudentId = function() {
return this.StudentId != null;
};
});
// Service
angular.service('StudentService', ['Student', '$resource', function(Student, $resource) {
var service = {};
service.student = $resource('student/:studentId'. {} { load: {method: 'GET'} });
service.loadStudent = function(studentId) {
var currentStudent = service.student.load(studentId); // HTTP call
currentStudent.$promise.then(function() {
// I expect the object currentStudent would have hasStudentId() prototype function, but it's not
angular.extend(currentStudent, new Student(currentStudent));
// However, if I use 'new' keyword, studentObj has prototype hasStudentId() function
var studentObj = new Student(currentStudent);
});
};
return service;
}]);
如你所见,在我扩展currentStudent
对象后,currentStudent没有原型函数hasStudentId()
。但是,如果我使用new Student(currentStudent)
,它具有正常的原型功能。
角。扩展忽略prototype
函数?
目前currentStudent
对象只具有$resource
的原型函数。
根据文档angular.extend
只复制自己的可枚举属性。原型属性可能是可枚举的,但它们不属于对象本身,因此不符合own的条件。MDN对此有一篇文章
相关文章:
- 使用“;这个“;JavaScript原型方法中的关键字
- Angularjs工厂注入错误
- 引用类变量中的原型方法
- 如何将参数传递到angularJs中的工厂
- 如何从对象的原型方法访问JavaScript对象属性
- 为什么要包装每一个原型“;类“;JS中具有匿名函数的对象
- 为什么只有工厂在棱角分明的网站上被提及
- Node.js中的JavaScript原型对象效率
- AngularJS:$q.dedefe()不能由工厂方法共享
- 重载JS'firefox中的对象原型
- Angular,函数在(模型)工厂中返回值
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 如何使用异步调用更改工厂的变量
- “util.inherits”和在NodeJS中扩展原型之间的区别
- 附加到原型属性的Do函数没有闭包
- 将变量传递到工厂
- AngularJS:多个工厂实例任何原型继承
- JavaScript 对象原型未通过工厂返回
- 来自工厂的原型中的方法出现了奇怪的错误,存储在控制器的$scope中
- 扩展$resource对象从工厂中移除原型