+ 4
Pls am confused can someone explain function, object, method in javascript
Pls with examples I have been unable to understand the concept of functions,object,method. But is it OK to move on with other stuff Then come back to revise on it
4 Respostas
+ 4
a method is a function inside an object:
function Point(x, y){
this.x = x;
this.y = y;
this.print = function(){
console.log("(" + this.x + "| " + this.y + ")");
}
}
calling a method works like this:
var p = new Point(0, 0);
p.print(); //output: (0| 0)
+ 3
Here is the answer to your question.
https://code.sololearn.com/W75DicSoPA69/?ref=app
+ 1
a function is sth you can call like this:
console.log();
->log is a function
they can return a value and help to orginise your code.
you can make your own functions like this:
function help(){
console.log("I need help");
}
//they can have parameters in the brackets and they can return a value
function double(n){
return 2 * n;
}
+ 1
a function that creates an Object is a constructor function they look similar, but they require the keyword 'this' to seperate object attributes from global variables or parameter:
function Point(x, y){
this.x = x;
this.y = y;
}
to create an Object we can simply do this:
var p = new Point(0, 0);
-> the new is the keyword for creating objects