Why there are so many types of import / export in Javascript / Typescript
I'm new to server side javascript, I've used nodejs before for simple things, but only default libraries (where I never need to use keywords require
or import
), but lately I'm learning ReactNative / ReactXP I've seen:
import RX = require('reactxp');
const popsicle = require('popsicle');
import LoginPage = require("./LoginPage");
import React, { Component } from 'react';
import { AppRegistry, Text } from 'react-native';
import AppState from './AppState';
And export:
export default Resources; // Resources is an object
export = LoginPage; // LoginPage is a class
The question is, what's the difference between combinations const-require
, import-require
and import-from
? also, what export=
seems not on the Mozilla doc ?
source to share
These are not fundamental differences: -
-
const
is new in ES5. Before you had to create it explicitly or with iffy. -
import
is new in ES6, which is a replacementrequire
(which depends on the generic js module). -
Export
is also an ES6 feature that allows you to usemodule
orobject/var
in another file.
So this is just a new convention and nothing. And now by default in Javascript after ES6.
It also {}
allows you to directly expose module / object properties as well as a new feature in ES6. For example: -
Suppose you have an object and in obj.js file:
export let objj1 = {
a : function () {},
b : function () {},
}
So basically there are two ways to use this 1.
let obj = require ('obj');
a = obj.a or b = obj.b;
OR
import {a, b} from 'obj'
So now you can redirect properties a and b.
source to share
import RX = require('reactxp');
import LoginPage = require("./LoginPage");
export = LoginPage; // LoginPage is a class
These 3 are typescript syntax import / export module .
const popsicle = require('popsicle');
These are nodejs modules require
.
Rest
import React, { Component } from 'react';
import { AppRegistry, Text } from 'react-native';
import AppState from './AppState';
export default Resources; // Resources is an object
ES2015 modules ( ).import
export
You cannot compare them: they are just imported / exported for different environments.
source to share