{"id":9565,"date":"2013-02-18T07:27:42","date_gmt":"2013-02-18T01:57:42","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9565"},"modified":"2016-12-19T15:31:01","modified_gmt":"2016-12-19T10:01:01","slug":"how-to-implement-inheritance-in-js2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-implement-inheritance-in-js2\/","title":{"rendered":"How to implement Inheritance in js2"},"content":{"rendered":"<p>We are assuming that you have installed <strong>js2<\/strong> in your system, in case if you have not installed <strong>js2<\/strong>, first install <strong><a href=\"http:\/\/www.tothenew.com\/blog\/getting-started-with-js2\/\">js2<\/a><\/strong>.<\/p>\n<p style=\"padding:10px\">\n<p>First of all create a InheritanceTest.js2 file in scr directory with the content given below:<br \/>\n[js]<br \/>\nclass Person { \/\/ Parent class<br \/>\n    var firstName;<br \/>\n    var lastName;<\/p>\n<p>    function initialize(firstName, lastName) { \/\/ Constructor<br \/>\n        this.firstName = firstName;<br \/>\n        this.lastName = lastName;<br \/>\n    }<\/p>\n<p>    function toString() {<br \/>\n        return this.firstName + &#8216; &#8216; + this.lastName;<br \/>\n    }<br \/>\n}<\/p>\n<p>class Employee extends Person { \/\/ Child class<br \/>\n    var companyName;<\/p>\n<p>    function initialize(firstName, lastName, companyName) { \/\/ Constructor<br \/>\n        this.$super(firstName, lastName);<br \/>\n        this.companyName = companyName;<br \/>\n    }<\/p>\n<p>    function toString() {<br \/>\n        return this.$super() + &#8216;, &#8216; + this.companyName;<br \/>\n    }<br \/>\n}<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p>To compile <strong>js2<\/strong> file into <strong>js<\/strong> 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>Now create a <strong>js<\/strong> script named as <strong>inheritance<\/strong>.<strong>js<\/strong> in scr directory with following content:<br \/>\n[js]<br \/>\nvar Person = require(&#8216;..\/lib\/InheritanceTest&#8217;).Person;<br \/>\nvar Employee = require(&#8216;..\/lib\/InheritanceTest&#8217;).Employee;<\/p>\n<p>var person = new Person(&#8220;Amit&#8221;, &#8220;Thakkar&#8221;);<br \/>\nconsole.log(person.toString())<br \/>\nvar employee = new Employee(&#8220;Amit&#8221;, &#8220;Kumar&#8221;, &#8220;Intelligrape&#8221;);<br \/>\nconsole.log(employee.toString())<br \/>\n[\/js]<\/p>\n<p style=\"padding:10px\">\n<p>Now run <strong>inheritance<\/strong>.<strong>js<\/strong> file with the command given below:<br \/>\n[bash]<br \/>\nnode src\/hello.js<br \/>\n\/\/ Output<br \/>\nAmit Thakkar<br \/>\nAmit Kumar, Intelligrape<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 js2 in your system, in case if you have not installed js2, first install js2. First of all create a InheritanceTest.js2 file in scr directory with the content given below: [js] class Person { \/\/ Parent class var firstName; var lastName; function initialize(firstName, lastName) { \/\/ Constructor this.firstName [&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":0},"categories":[1],"tags":[1134,55,1130,1124],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9565"}],"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=9565"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9565\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}