我有一个构造函数,它注册一个事件处理程序:
function MyConstructor(data, transport) {
this.data = data;
transport.on('data', function () {
alert(this.data);
});
}
// Mock transport object
var transport = {
on: function(event, callback) {
setTimeout(callback, 1000);
}
};
// called as
var obj = new MyConstructor('foo', transport);Run Code Online (Sandbox Code Playgroud)
但是,我无法data在回调中访问已创建对象的属性.它看起来this并不是指创建的对象,而是指另一个对象.
我还尝试使用对象方法而不是匿名函数:
function MyConstructor(data, transport) {
this.data = data;
transport.on('data', this.alert);
}
MyConstructor.prototype.alert = function() {
alert(this.name);
};
Run Code Online (Sandbox Code Playgroud)
但它表现出同样的问题.
如何访问正确的对象?
我有服务,说:
factory('aService', ['$rootScope', '$resource', function ($rootScope, $resource) {
var service = {
foo: []
};
return service;
}]);
Run Code Online (Sandbox Code Playgroud)
我想foo用来控制用HTML呈现的列表:
<div ng-controller="FooCtrl">
<div ng-repeat="item in foo">{{ item }}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
为了让控制器检测何时aService.foo更新,我将这个模式拼凑在一起,我将aService添加到控制器$scope,然后使用$scope.$watch():
function FooCtrl($scope, aService) {
$scope.aService = aService;
$scope.foo = aService.foo;
$scope.$watch('aService.foo', function (newVal, oldVal, scope) {
if(newVal) {
scope.foo = newVal;
}
});
}
Run Code Online (Sandbox Code Playgroud)
这感觉很长,我一直在每个使用服务变量的控制器中重复它.有没有更好的方法来完成观察共享变量?
在JavaScript文件中,我看到:
function Somefunction(){
var that = this;
...
}
Run Code Online (Sandbox Code Playgroud)
声明that和分配给它的目的是什么this?
使用实例方法作为事件处理程序的回调改变的范围this从"我的实例"到"无论只是调用的回调".所以我的代码看起来像这样
function MyObject() {
this.doSomething = function() {
...
}
var self = this
$('#foobar').bind('click', function(){
self.doSomethng()
// this.doSomething() would not work here
})
}
Run Code Online (Sandbox Code Playgroud)
它有效,但这是最好的方法吗?这对我来说很奇怪.
每个人都知道this在javascript中,但也有self在野外遇到的实例,如在这里
那么,JavaScript this和selfJavaScript 之间有什么区别?
我在几乎所有的knockout.js视图模型中看到了这一行var self = this,然后所有局部变量都被引用为self.variableName.这比使用有this.variableName什么好处?
在这段代码片段中,为什么它this.identifier不起作用但_self.url有效?
getConfig() {
let _self = this;
return function () {
this.page.url = this.url || window.location.href;
this.page.identifier = _self.identifier;
this.page.category_id = this.categoryId;
this.language = this.lang;
};
}
Run Code Online (Sandbox Code Playgroud)
因此,没有let _self = this真正做?
我很困惑,什么时候使用自我和这在JavaScript中.
我知道这是指当前的上下文,而self指的是当前窗口.
当我在Titanium中开发应用程序时.我想知道何时使用自我或者这个
或者确实在钛开发中有任何自我概念.
这是我在钛commonJS模块中执行的示例代码
var auth = require('/SDKTest/auth');
var nodeAPI = require('/SDKTest/nodeAPI');
function myAPI() {
this.auth = auth;
this.nodeAPI = nodeAPI;
return this;
}
module.exports = myAPI;
Run Code Online (Sandbox Code Playgroud)
此代码的工作,但我可以用自己代替这个?而不是使用这个,我可以创建一个命名空间,并做这样的事情:
function myAPI() {
var api = {};
api.auth = auth;
api.nodeAPI = nodeAPI;
return api;
}
Run Code Online (Sandbox Code Playgroud)
这两种方法都有效,但在这里使用它的用途是什么
我厌倦了编写这样的代码:
class Something {
constructor() {
this.method = this.method.bind(this);
this.anotherOne = this.anotherOne.bind(this);
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
很费时间,而且很容易忘记绑定一个方法。我知道 class fields 提案,但它仍然是第 3 阶段,似乎有一些问题。
我当前的解决方案(基于此答案)如下所示:
class Something {
constructor() {
// Get all defined class methods
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this));
// Bind all methods
methods
.filter(method => (method !== 'constructor'))
.forEach((method) => { this[method] = this[method].bind(this); });
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但我想知道是否有更好的方法,或者此方法是否存在我不知道的问题。
我遇到的问题是,如果我没有在构造函数中绑定我的类函数,我必须记住以后“正确地”调用它们。例如:
const classInstance = new Something();
// This fails for a non-obvious reason
someAction()
.then(classInstance.method);
// This …Run Code Online (Sandbox Code Playgroud) 名称+复选框的单向绑定工作正常,但它最初不适用于单选按钮employeeTypeA,虽然它在viewmodel中的值为true,html显示单选按钮未设置,为什么会这样?
<script type="text/javascript">
$(function()
{
var PersonViewModel = function()
{
this.firstName = ko.observable('Lisa');
this.lastName = ko.observable('T');
this.isCustomer = ko.observable(true);
this.employeeTypeA = ko.observable(true);
this.employeeTypeB = ko.observable(false);
};
var personViewModel = new PersonViewModel();
ko.applyBindings(personViewModel, $('data').get(0));
});
</script>
<div id="data">
<span data-bind="text: firstName"></span>
<span data-bind="text: lastName"></span>
<input type="checkbox" data-bind="checked: isCustomer" title="Is a customer" />
<input name="x" type="radio" data-bind="checked: employeeTypeA" title="Employee type A" />
<input name="x" type="radio" data-bind="checked: employeeTypeB" title="Employee type B" />
</div>
Run Code Online (Sandbox Code Playgroud) javascript ×8
knockout.js ×2
this ×2
angular ×1
angularjs ×1
arrays ×1
callback ×1
closures ×1
ecmascript-6 ×1
function ×1
jquery ×1
object ×1
scope ×1
titanium ×1
typescript ×1
watch ×1