+ 1

Hello, im asking this question regarding what i learn JS. Now im learning about method. And here the code from SoloLearn

function person(name, age) { this.name= name; this.age = age; this.yearOfBirth = bornYear; } function bornYear() { return 2016 - this.age; } var p = new person("A", 22); document.write(p.yearOfBirth()); can somebody explain what the hack with 1.function bornYear() 2. document.write(yearOfBirth()) How to understand that input function? () is it input symbol right? im new here

9th Oct 2018, 3:43 AM
Zulfadhli
Zulfadhli - avatar
3 Respuestas
+ 1
Parentheses "()" in function declaration is used to accept argument(s), all arguments passed to a function are written inside the parentheses, each separated by comma. Arguments can be object, values or expression that can be used by the function in its code to accomplish its task, or alter how the function works. Example: function add(a, b) { return a + b; } a & b are arguments, result of a + b here is called return value, because it is passed back (returned) by "return" statement.
9th Oct 2018, 9:41 AM
Ipang
+ 3
Hi Zulfadhli, Looks like you are new to programming. I recommend you to try and build your own code from scratch. And then try to implement some things. With the "function" keyword you define a new function, after the whitespace you can give the function a name. after the function name you have to place (). In there you can give the function variables, Variables passed in a function are called parameters. So in your script you got a function called person which takes 2 parameters; name and age. and you got a function called bornYear which takes no parameters. you can call a function by its name and the () with its parameters(if it takes any) so if you want to call the function bornYear just do: bornYear(); if you want to call the function person do: person(var1, var2); Notice that you need to pass the 2 variables to the function. These variables you pass in the function call will become the parameters in your function. So what does your function bornYear do?? It just returns the value of (2018-this.age) What will give an error because it does't know this.age since its outside of the scope of the person function where this.age is defined. document.write() is a function to write to a file. whats inside the () is what you will write. With the line var p = new person("A", 22); You make a new instance of the function person as p. I belief only javascript is a language without classes. Normally you would make an instance of a class you made(which will be called an object(of the class)) With the p.yearOfBirth() you call the method "yearOfBirth" on the instance p of the function person. Which also will give an error because yearOfBirth is a variable of the function person and not a method(method = a function inside a class) of the function person.
9th Oct 2018, 10:09 AM
Willem Roos
+ 1
function bornYear calculates the birth year of the person object created (named "A", 22 years old) by subtracting 2016 with the age property. The "yearOfBirth" property of the person object refers to bornYear function, and as such when we write "p.yearOfBirth()" we are actually calling the bornYear function and writing the return value given back by bornYear function. document.write() is a method (a method is a function defined in a class) belonging to the document object. Its purpose is to write (output) something in the document that is currently loaded in browser.
9th Oct 2018, 9:40 AM
Ipang