Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing object properties in other scripts

Tags:

javascript

I'm trying to use some function defined in a file inside a HTML script tag.

Here is the javascript file myfunction.js:

fct = function() {
};

Inside index.html I have:

<script src="./myfunction.js"></script>
<script>
  fct.some.property = function() {
  }
</script>

It says fct.some is undefined on the console. How to define some inside fct so that I can use it in oter scripts ? I don't want to use any librairies.

I tried the following but it does not work :

fct = function() {
  return {
    some: {}
  }
};
like image 724
jerome Avatar asked Jun 03 '26 20:06

jerome


1 Answers

I can't reproduce the error; when I test your code as-is I get

fct.some is undefined

The fix is:

fct.some = {};
fct.some.property = function() {
}

This works fine for me.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!