Categories
discuss

JS import module and run on page load

I want to call my function main() using html onload event and console.log text imported from another (generateObject.js) file, but when I import function, onload event stop working and function main() is not anymore used.

html:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="main.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>
  <body onload="main()">
  </body>
</html>

generateObject.js:

export function hello() {
    return "Hello";
}

main.js:

import { hello } from './generateObject.js';
function main(){
      console.log(hello());
}

main();

When I try console.log(“text”) in main() it works but when I try to use imported function it’s not. What should I do to fix that?

Errors in Chrome console:

Uncaught SyntaxError: Cannot use import statement outside a module (main.js:1)

index.html:8 Uncaught ReferenceError: main is not defined at onload (index.html:8)

Answer

modules will have its own scope. They are not available in the global scope like the normal scripts. So it’s accessible only inside main.js in your case.

To make it work you need to add it to global scope explicitly.

import { hello } from './generateObject.js';
function main(){
      console.log(hello());
}

window.main = main;

Alternatively, you can remove the event handler from HTML and add it in the JS file.

html

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="main.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>
  <body>
  </body>
</html>

main.js

import { hello } from './generateObject.js';
function main(){
      console.log(hello());
} 

window.addEventListener('load', main)
Categories
discuss

Java stream question, mapToInt and average method

Why can I call average() method on one but not on the other? Shouldn’t they be equivalent?

example 1 – works

List<String> stringList = new ArrayList<>();
    stringList.add("2");
    stringList.add("4");
    stringList.add("6");
// String array ("2","4", "6"
averageValue = stringList.stream()
                .mapToInt(s -> Integer.valueOf(s))
                .average()
                .getAsDouble();   

example 2 – doesn’t compile (deleted mapToInt call because already passing Integer stream)

List<Integer> IntegerList = new ArrayList<>();
        IntegerList.add(2);
        IntegerList.add(4);
        IntegerList.add(6);

averageValue = IntegerList.stream()
                    .average()
                    .getAsDouble();  

Question, is why do I need to call mapToInt method when Im already passing it a stream of Integers?

Answer

There are two different types: a Stream<Integer> and an IntStream.

Java’s generics can’t have methods that only apply on some generics. For example, it couldn’t have Stream<Integer>.average() and not also have Stream<PersonName>.average(), even though the average person name doesn’t make sense.

Therefore, Stream has a mapToInt method that converts it into an IntStream, which then provides the average() method.

Categories
discuss

unity (Android bild), i have problem “We can’t assign a new GUID because the asset is in an immutable folder. The asset will be ignored.”

I have unity 2019.3.7f1, 2 sided libraries:

GooglePlayGamesPlugin-0.10.08
GoogleMobileAds-v3.18.3

(it also installed “latest External Dependency Manager for Unity”)

And everything compiled perfectly on Android (apk)

I added a library Firebase 6.13.0 and got the following errors: Err I read that you need to delete them and restart the project, it did not help, I also reinstalled all the libraries, it did not help.

Has anyone had similar problems? Please help solve it.

Answer

It helped me:

  • Update Unity to 2019.4.15f
  • Update Firebase to 6.16.1
  • Update GooglePlayGamesPlugin-0.10.11
  • Update GoogleMobileAds-v5.4.0 (optional)
  • Remove Assets/ExternalDependencyManager and Assets/PlayServicesResolver folder
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..