简体   繁体   English

如何在JavaScript中调用方法onload

[英]How to call a method onload in JavaScript

So I want to call a function Local.getThis onload with this code: 所以我想用以下代码调用函数Local.getThis onload:

    class Local {
        getThis() {
            let that;
            if (localStorage.getItem('that') === null) {
                that = [];
                console.log(that);
                localStorage.setItem('that', that);
            } else {
                that=JSON.parse(localStorage.getItem('that'));
                console.log(that);
            }
        }

    // DOM Load Event 
    document.addEventListener('DOMContentLoaded', Local.getThis)

But nothing is happening, no error nothing. 但是什么也没有发生,没有错误也没有。 But when I change "getThis" to STATIC it works (Output: []). 但是,当我将“ getThis”更改为STATIC时,它可以工作(输出:[])。 Does it need to be STATIC ?? 它需要是静态的吗?

PS PS

After setting that = []; 设置= []后; I get an error 我得到一个错误

'Uncaught SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at HTMLDocument.getThis'

on the next reload but that is probably a whole other problem I guess. 在下一次重新加载时,我想那可能是另一个问题。

EDIT: 编辑:

For the record the error was related to localStorage.setItem('that', that); 为便于记录,该错误与localStorage.setItem('that', that); , it should be ofcourse localStorage.setItem('that', JSON.stringify(that)); ,当然应该是localStorage.setItem('that', JSON.stringify(that));

part 1: this is a reserved word in JavaScript, change parameter's name. 第1部分: 是JavaScript中的保留字,请更改参数的名称。

part 2: Local is a class, so to access a function directly from it that function must be static. 第2部分:Local是一个类,因此要直接从该函数访问函数,该函数必须是静态的。 Otherwise an instance needs to be initiated first. 否则,需要首先启动实例。 and the function can then be used from that instance. 然后可以从该实例使用该函数。

You should create an instance of Local class first to have this 您应该先创建Local类的实例才能具有this

const local = new Local();
document.addEventListener('DOMContentLoaded', local.getThis.bind(local))
//or
document.addEventListener('DOMContentLoaded', function() {
  local.getThis();
})

Update 更新

Also you cannot use variable name this as it is reserved word. 你也不能使用变量名this ,因为它是保留字。 If you don't need a context ( this ) you can create a method as a static method: 如果不需要上下文( this ),则可以将方法创建为静态方法:

class Local{}
Local.getThis = function() {
 // code without this
}

so then you can use as you've written: 因此,您可以使用已编写的内容:

document.addEventListener('DOMContentLoaded', local.getThis);

But nothing is happening, no error nothing. 但是什么也没有发生,没有错误也没有。 But when I change "getThis" to STATIC it works (Output: []). 但是,当我将“ getThis”更改为STATIC时,它可以工作(输出:[])。 Does it need to be STATIC ?? 它需要是静态的吗?

To call your method as Local.getThis need to be static . 要将您的方法称为Local.getThis,必须为static

Otherwise you need to create an instance of Local. 否则,您需要创建一个 Local 实例

var local = new Local();
document.addEventListener('DOMContentLoaded', function() {
  local.getThis();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM