{"id":9537,"date":"2013-02-16T15:22:11","date_gmt":"2013-02-16T09:52:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9537"},"modified":"2016-12-19T15:30:59","modified_gmt":"2016-12-19T10:00:59","slug":"getting-started-with-js2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-started-with-js2\/","title":{"rendered":"Getting started with js2"},"content":{"rendered":"<p>We are assuming that you have installed <strong>node.js<\/strong> in your system, in case if you have not installed <strong>node.js<\/strong>, first install <strong><a href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-node-js-on-linux\/\">node.js<\/a><\/strong>.<br \/>\nTo install <strong>js2<\/strong> run the command given below:<br \/>\n[bash]<br \/>\nnpm install js2<br \/>\n[\/bash]<\/p>\n<p style=\"padding:10px\">\n<p>Let\u2019s create a simple <strong>Hello<\/strong> <strong>World<\/strong> project, but first let\u2019s create a project directory structure:<br \/>\n[bash]<br \/>\nmkdir helloWorldJs2<br \/>\ncd helloWorldJs2<br \/>\nmkdir scr lib<br \/>\n[\/bash]<\/p>\n<p style=\"padding:10px\">\n<p>Now create a <strong>HelloWorld<\/strong>.<strong>js2<\/strong> file in scr directory with the content given below:<br \/>\n[js]<br \/>\nclass HelloWorld {<br \/>\n  function sayHelloWorld() {<br \/>\n    console.log(&#8220;Hello World&#8221;);<br \/>\n  }<br \/>\n}<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p>To compile <strong>js2<\/strong> file into js file in lib directory run the command given below:<br \/>\n[bash]<br \/>\njs2-node compile -f=node .\/src .\/lib<br \/>\n[\/bash]<\/p>\n<p style=\"padding:10px\">\n<p>Above command will compile <strong>HellowWorld<\/strong>.<strong>js2<\/strong> file and will generate <strong>HelloWorld<\/strong>.<strong>js<\/strong> file in lib directory. Now create a js script named as <strong>hello<\/strong>.<strong>js<\/strong> in scr directory with following content:<br \/>\n[js]<br \/>\nvar HelloWorld = require(&#8216;..\/lib\/HelloWorld&#8217;).HelloWorld;<br \/>\nvar helloWorld = new HelloWorld();<br \/>\nhelloWorld.sayHelloWorld();<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p>Now run <strong>hello<\/strong>.<strong>js<\/strong> file with the command given below, And you will get <strong>Hello<\/strong> <strong>World<\/strong> printed at console.<br \/>\n[bash]<br \/>\nnode src\/hello.js<br \/>\n[\/bash]<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are assuming that you have installed node.js in your system, in case if you have not installed node.js, first install node.js. To install js2 run the command given below: [bash] npm install js2 [\/bash] Let\u2019s create a simple Hello World project, but first let\u2019s create a project directory structure: [bash] mkdir helloWorldJs2 cd helloWorldJs2 [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1131,1133,55,1130,1124,1132],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9537"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9537"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9537\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}