javascript - <div onclick="fun()"></div> Is this kind of fun function only valid if it is written as a global function?
欧阳克
欧阳克 2017-06-30 09:59:44
0
1
679
<p onclick="fun()"></p>

It seems that event functions can only be written in the global scope. What if I want to call methods in the class?

class xxx(){
    func(){}//如果要调用这个方法呢?不能直接写在onclick后面吧
}
欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

reply all(1)
淡淡烟草味

If you want to call a function in a class, you have to instantiate it

let x = new xxx()
<p onclick="x.fun()"></p>

First of all, you have to understand that class is just syntactic sugar for the constructor, which is equivalent to

var xxx = (function () {
    function xxx() {}
    xxx.prototype.func = function () { };
    return xxx;
}());

Uh, what does that () after class mean? New syntax?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template