Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- javascript methods
- types:
- 1 method with return and with parameter
- 2 method without return and wothout parameter
- 3 method with return and wothout parameter
- 4 method without return and with parameter
- */
- // type 1
- function add(a,b) {
- return a+b;
- }
- // calling the function by holding
- h = add(1,2);
- console.log(h);
- // calling the function direct to print in console
- console.log(add(1,2));
- // type 2
- function pi(){
- console.log(3.14);
- }
- // calling function
- pi();
- console.log("--------scope--------");
- // scope of variable
- var a = 10;
- function safe(){
- var a = 20;
- var b = 10;
- c = 30;
- console.log(a);
- console.log(b);
- console.log(c);
- }
- safe();
- console.log(a);
- // console.log(b) // this is not accessable outside of the function
- console.log(c);
- console.log("--------block statement--------");
- {
- var a = 13;
- }
- console.log(a);
- {
- let e = 12;
- console.log(e);
- }
- // console.log(e) // this is not accessable outside the block
- console.log("--------constant--------");
- const pit = 3.14;
- var ti = 21;
- console.log(ti,pit);
- var ti = 22;
- // var pit = 22/7;
- console.log(ti,pit)
- {
- let gb = 12;
- console.log(gb);
- }
- {
- var gb = 13;
- console.log(gb);
- // var pit = 22/7;
- }
- console.log("--------class and object--------");
- /*
- SYNTAX :
- class classname{
- functionname(para){
- return;
- }
- }
- object = new classname();
- */
- class sample{
- name = "jack" // it is a class variable not a global variable
- print() {
- return this.name
- }
- }
- s = new sample();
- var a = s.print();
- console.log(a);
- console.log(s.name)
- console.log("--------polymorphism and overloading--------");
- class animal{
- dog(breed){
- console.log("i am one",breed);
- }
- dog(breed,paw){
- console.log("i am two",breed,paw);
- }
- }
- a = new animal();
- console.log(a.dog("dober"));
- console.log(a.dog("dober",2));
- class animal_one{
- dog(breed){
- console.log("i am one",breed);
- }
- }
- class animal_two{
- dog(breed,paw){
- console.log("i am two",breed,paw);
- }
- }
- b = new animal_one();
- console.log(b.dog("dober"));
- c = new animal_two();
- console.log(c.dog("dober",2));
- console.log("--------constructor--------");
- class one{
- constructor(a,b,c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- basic(){
- console.log(this.a,this.b,this.c);
- }
- }
- d = new one(1,2,3);
- d.basic();
- class two{
- constructor(){
- this.a = 1;
- this.b = 2;
- this.c = 3;
- }
- basic_2(){
- console.log(this.a,this.b,this.c);
- }
- }
- e = new two();
- e.basic_2();
- console.log("--------inheritance and overrideing--------");
- class anml{
- dog(){
- console.log("wof");
- }
- cat(){
- console.log("mew");
- }
- }
- class pet extends anml{
- dog(){
- console.log("bow");
- }
- }
- o = new anml();
- o.dog();
- o.cat();
- p = new pet();
- p.dog(); // over rided
- p.cat();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement