简体   繁体   中英

How to use Javascript to call an external library's function inside of a function?

Hello so I am using the CryptoJS library to do a SHA256 hash. I am having the problem where it allows me to do something like this.

<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>

<script> var p = CryptoJS.sha256("password"); </script>

but not this

<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js"></script>

<script>
 function login() {
 var p = CryptoJS.sha256("password");
 } </script>

This gives me an undefined function error.

I am new to JavaScript... so I may just not be that familiar with scopes and external libraries could someone help clarify this for me?

If you call the function login() as you have it written all that will happen is that a hash of the word "password" will be generated and assigned to the variable p. -- and that is all!

If you want a working function you need to do something with this variable p. You could start by looking at what is being generated by adding console.log(p) inside the function and taking a look at your console.

And if all you are looking for is the hash of "password" you can return p .

Good luck!

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