this

How to access the correct `this` inside a callback?

那年仲夏 提交于 2020-06-29 04:34:18
问题 I have a constructor function which registers an event handler: 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); However, I'm not able to access the data property of the created object inside the callback. It looks like this does not refer to the object that

Angular2 Custom Form Validators losing access to `this` in class

青春壹個敷衍的年華 提交于 2020-06-22 13:30:11
问题 I'm building an Angular 2 application (2.0) and I've got a form that makes use of the ReactiveForms module. As such, I'm using validator functions on the input fields, including custom validators. This works fine. My issue is that I am also trying to use a custom AsyncValidator on a form field. It seems straight forward enough, but I can never reference properties inside the class from within the AsyncValidator method. The FormGroup and the FormControl properties for the form itself and the

Factory pattern using unique_ptr in c++

China☆狼群 提交于 2020-05-25 05:31:15
问题 I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class A , and a derived class B . In main() , I pass 1 to the create method in A , and the type of b1 is now changed to B . #include <iostream> #include <map> class A { public: A() {} virtual void Foo() {} std::map<int, A *> &registerType() { static std::map<int, A *> map_instance; return map_instance; } A *create(int n) {

Factory pattern using unique_ptr in c++

允我心安 提交于 2020-05-25 05:31:10
问题 I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class A , and a derived class B . In main() , I pass 1 to the create method in A , and the type of b1 is now changed to B . #include <iostream> #include <map> class A { public: A() {} virtual void Foo() {} std::map<int, A *> &registerType() { static std::map<int, A *> map_instance; return map_instance; } A *create(int n) {

really confused by “enclosing scope” of javascript es6 arrow function

谁说胖子不能爱 提交于 2020-05-08 03:39:07
问题 I did a lot research online, read many posts including MDN and so on. I understand that for traditional defined functions, "this" within functions is defined by objects calling/invoking them (and several different cases, object literal, new constructor, event handler, etc.). I understand that for arrow functions, "this" is defined lexically, by the enclosing context/scope, not by objects invoking them (though we can use a traditionally defined function (say, A) to wrap arrow functions (say, B

一道常被人轻视的前端JS面试题

空扰寡人 提交于 2020-05-07 19:18:11
前言 年前刚刚离职了,分享下我曾经出过的一道面试题,此题是我出的一套前端面试题中的最后一题,用来考核面试者的JavaScript的综合能力,很可惜到目前为止的将近两年中,几乎没有人能够完全答对,并非多难只是因为大多面试者过于轻视他。 题目如下: JavaScript function Foo() { getName = function () { alert (1); }; return this; } Foo.getName = function () { alert (2);}; Foo.prototype.getName = function () { alert (3);}; var getName = function () { alert (4);}; function getName() { alert (5);} //请写出以下输出结果: Foo.getName(); getName(); Foo().getName(); getName(); new Foo.getName(); new Foo().getName(); new new Foo().getName(); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function Foo ( ) { getName = function ( ) { alert ( 1 )

Can I use “this” in mapMutations spread inside Vue instance methods?

假装没事ソ 提交于 2020-04-10 07:27:09
问题 I want to set Vuex mutations as follows: export default { props: { store: String }, methods: { ...mapMutations({ changeModel: `${this.store}/changeModel` }) } } But I catch the error: Uncaught TypeError: Cannot read property 'store' of undefined How do I correctly use props inside the module mutation name? I want to map this.$store.commit('form1/changeModel') , where form1 is set from props . 回答1: Vuex helper mapMutations can be used with a function which works with this . There does not seem

java中super 的两种用法

梦想的初衷 提交于 2020-04-10 03:14:09
通过用 static 来定义方法或成员,为我们编程提供了某种便利,从某种程度上可以说它类似于 C 语言中的全局函数和全局变量。但是,并不是说有了这种便利,你便可以随处使用,如果那样的话,你便需要认真考虑一下自己是否在用面向对象的思想编程,自己的程序是否是面向对象的。 好了,现在开始讨论 this&super 这两个关键字的意义和用法。 在 Java 中, this 通常指当前对象, super 则指父类的。当你想要引用当前对象的某种东西,比如当前对象的某个方法,或当前对象的某个成员,你便可以利用 this 来实现这个目的,当然, this 的另一个用途是调用当前对象的另一个构造函数,这些马上就要讨论。如果你想引用父类的某种东西,则非 super 莫属。由于 this 与 super 有如此相似的一些特性和与生俱来的某种关系,所以我们在这一块儿来讨论,希望能帮助你区分和掌握它们两个。 在一般方法中 最普遍的情况就是,在你的方法中的某个形参名与当前对象的某个成员有相同的名字,这时为了不至于混淆,你便需要明确使用 this 关键字来指明你要使用某个成员,使用方法是 “this. 成员名 ” ,而不带 this 的那个便是形参。另外,还可以用 “this. 方法名 ” 来引用当前对象的某个方法,但这时 this 就不是必须的了,你可以直接用方法名来访问那个方法

PHP copy all object properties to this

▼魔方 西西 提交于 2020-04-07 14:47:04
问题 I have an object in PHP, of the type MyObject . $myObject instanceof MyObject Now, in the class MyObject , there is a non-static function, and in there, I use the reference to "me", like $this , but I also have another object there. Is it possible, without doing $this = $myObject , to achieve more or less the same effect, like something of the sort set_object_vars($this, get_object_vars($myObject)) ? 回答1: <?php class MyObject { public function import(MyObject $object) { foreach (get_object

PHP copy all object properties to this

≯℡__Kan透↙ 提交于 2020-04-07 14:46:45
问题 I have an object in PHP, of the type MyObject . $myObject instanceof MyObject Now, in the class MyObject , there is a non-static function, and in there, I use the reference to "me", like $this , but I also have another object there. Is it possible, without doing $this = $myObject , to achieve more or less the same effect, like something of the sort set_object_vars($this, get_object_vars($myObject)) ? 回答1: <?php class MyObject { public function import(MyObject $object) { foreach (get_object