我正在比较在 Knockout.js中使用计算的 observable 函数的两种方法
问题:
- 函数中的“this”关键字可以引用它的父对象(外部,而不是内部的函数)吗?
为什么即使没有将上下文值放在最后,版本 2 也能正常工作?
///Version 1 my.Product = function () { this.id = ko.observable(); this.salePrice = ko.observable(); this.photo = ko.observable(); this.shortDescription = ko.observable(); this.photoUrl = ko.computed (function () { return photoPath + this.photo(); },this); //**context** }; ////version 2 my.Product = function () { var self = this; self.id = ko.observable(); self.salePrice = ko.observable(); self.photo = ko.observable(); self.shortDescription = ko.observable(); self.photoUrl = ko.computed(function () { return photoPath + self.photo(); });//why there is no "self" here };