문제

I've got an object defined as a function, and it's got a lot of methods and properties in it, what are some ways to modularise this to make it more manageable? It is possible to put functions inside the object into external files?

Edit:

Someone mentioned in one answer to include other files, but this would have an unweildy HTML page, IE:

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
....
<script type="text/javascript" src="script76.js"></script>

Is there also any way to structure the code so only one reference is required the the javascript file?

도움이 되었습니까?

해결책

var myobj = {
  somefunc: function() { /* function code */ },
  prop: "some string",
  // etc.
};

and in another js file included after the above code

myobj.anotherfunc = function () { /* function code */ };

다른 팁

You may want to check out RequireJS. It lets you define functions, modules, dependencies and load external Javascript files from Javascript (possibly async).

you could use a server side script to handle your javascripts, with jsmin you could even speed up the process (if cached).

i'm thinking of a script somthing like this: scripts.php

<?php 

$files = array('script1.js', 'script2.js');
$output = '';

foreach($files as $file){
    $output .= file_get_contents($file);
}

header("Content-type: application/x-javascript");
echo $output;

?>

your html
<script type="text/javascript" src="/script.php"></script>

you could also just include all js files inside a folder, just generate the array via folder scanning or something, i think you get the idea!

greetings Nexum

You could try this:
http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm

...but best practices are to just include the files you want in the main html page

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top