简体   繁体   中英

How to put a dot in a function (ex.: lib.genarate() )

How can I put a dot in a function in javascript? I have searched, but nothing!
For example:
lib.execute(); or hello.greet();

I tried:

 function hello.world() { document.write('Hello world'); }
 <button onclick="hello.world();">Click me</button>

And I (obviously) receive this:

 Error: {
  "message": "Uncaught ReferenceError: go is not defined",
  "filename": "https://stacksnippets.net/js",
  "lineno": 12,
  "colno": 29
}

I have seen somewhere that it used variables, but it was not docs and I do not remember how to use it

The correct way of doing as you wish in your example would be something like:

let go = {
    abc: function() {
        document.write('Hello world!');
    }
}

You need to create a class, or an object that act as a class, and then you can create properties as functions on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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