The proposal
The include expression includes and evaluates the specified file.
Why have this idea?
- Languages like PHP, Python, Javascript with Nodejs are great starting points for building modern websites. Of course, there are several other languages like Ruby, C# and others that can make modern websites too. But I said only the languages I know and use. If I were to list it would be a very long list.
- Part of my idea is to implement the word include which is the reserved word in PHPfor including files. I would like to have an include keyword in Javacript to import files at runtime without having to keep doing export and import. Keeping typing import/export is trivial. Languages like PHP already do this, languages like Python too.
- So part of my idea is to do something that is already possible in languages like PHP, Python to Javacriptnatively. Which will facilitate the development of algorithms in the language Javacript.
- An interesting point, there are several frameworks that do this. Which reinforces my argument that this idea is something interesting and relevant to the Javascript community.
sample 1: only syntax
include './a.js'; // Paths are relative to the current file.
include './a.js'; // Paths are relative to the current file.
include 'b.js'; // This is equivalent './b.js'.
include './c'; // If no extension is found, '.js' will be used.
include "./d.js"; // Single or double quotes are supported.
include '../e.js'; // Upwards directory traversal is supported.
include './f';
include './f.js'; // Duplicates will only be included once.
include './*.js'; // node-glob patterns are supported.
sample 2: variables_test1.js
var color = 'green';
sample 2: variables_test2.js
include './variables_test1.js';
console.log(color); // Output: color green
Some advantages
- You don't need to import or export variables, all this is automatic like php
- The include works at runtime
- This can facilitate the migration of PHP or Python developers to JavaScript
- A resemblance to the word reserved Include in PHP / Python
- If there is any errors in include, it stops running the script.
- You have the warranty that the file will not be included again if it has already been included before.
before: script1.js
export default function Hello() {
return "Hello buddy!";
}
before: script2.js
import { Hello } from './script1.js';
let val = Hello;
console.log(val);
after: script1.js
function Hello() { // without export default
return "Hello buddy!";
}
function Hello2(name) { // without export default
return "name";
}
after: script2.js
include './script1.js'; // without import from
let name = "willow"
console.log(Hello());
console.log(Hello2(name));
References
- GitHub - phpv8/php-v8: PHP extension for V8 JavaScript engine
- GitHub - hirak/phpjs: php.js implements PHP functions in JavaScript
- GitHub - locutusjs/locutus: Bringing stdlibs of other programming languages to JavaScript for educational purposes
- GitHub - timrwood/includer: Concatenate JavaScript files with inline include statements.
- PHP: include - Manual
- include.js ยท GitHub
- GitHub - require-x/python-js: Use Python in JavaScript.
- GitHub - PythonJS/PythonJS: PythonJS development has moved to Rusthon
- GitHub - majwilson/python-to-javascript: A tool that allows programmers to convert code written in Python into the equivalent code written in Javascript.
- GitHub - majwilson/python-to-javascript: A tool that allows programmers to convert code written in Python into the equivalent code written in Javascript.
- GitHub - flexxui/pscript: Python to JavaScript compiler
- import - JavaScript | MDN
- import one JS file into another in the plain JS | Web Design and Web Development news, javascript, angular, react, vue, php