javascript - What is the difference between Methods and Functions? How to declare a method? -


i learning javascript on codecademy. understanding method function associated objects. think call method should inside object. it? should understand main difference between functions , methods write error free code. confusing me.

below codecademy code, on line 2 'setage' method looks function. not related object yet. coz not inside of object.

// here define our method using "this", before introduce bob var setage = function (newage) {   this.age = newage; };  // make bob var bob = new object(); bob.age = 30; bob.setage = setage;  // make susan here, , first give age of 25 var susan = new object(); susan.age = 25; susan.setage = setage; susan.setage(35); // here, update susan's age 35 using method 

that's question, see how confusing. appears referring method because later modify bob object include function, thereby making "method".

var setage = function (newage) {   // using "this" indicates   // function may instead object method   this.age = newage; };  // becomes method bob.setage = setage; 

their code equivalent following:

bob.setage = function (newage) {   this.age = newage; }; 

you right in understanding methods.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -