Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts
Original source
33 responses to “Beginner JavaScript Tutorial – 27 – Adding Methods to Our Objects”
Why isn't my browser processing this ?
?
this syntax is … so alienish
i did:
function people(year) {
this.year = year;
this.yourage = getage;
}
function getage() {
return 2017 – this.year;
}
var hamid = new people(2002);
document.write(hamid.yourage);
it gave me this error at the browser pls help:
function getage() { return 2017; }
Ummm… Why is yearsLeft() not encapsulated (not even sure if it's the right term) inside people
class?from my experiment, not putting yearsLeft() inside people
classfunction means that function yearsLeft() can be called from anywhere by anyone, and thus creating unexpected and undesirable result—especially when playing with "this" keyword.putting yearsLeft() inside people
classstraight up preventing other irrelevant things from accessing yearsLeft() function without instantiating peopleclassfirst.I don't know whats going on the 100 is not subtracting this.agehere is what I did<html>
<head>
<script type="text/javascript">
function people(name,age){
this.name = name;
this.age = age;
this.YearsUntilDeath = yearsLeft;
}
function yearsLeft(){
numYears = 100 – this.age;
return = numYears;
}
var youtube = new people("Youtube channel" , 70);
</script>
</head>
<body>
<script type="text/javascript">
document.write(youtube.yearsUntilDeath());
</script>
</body>
</html>
now am getting lost
he is giving best tutorials but i'm bored about objects and all that stuff 😴
This guy's really got some issues with phlegm.
your code is wrong.
Can you do this.yearsLeft = yearsLeft? Or do they have to be different in this case?
What's wrong with this code? I keep getting the lines of code from function UrAgeMinusFive?
function person(name, age) {
this.name = name;
this.age = age
this.age2 = UrAgeMinusFive;
}
var a = new person("Bill", 25);
var b = new person("Mark", 28);
function UrAgeMinusFive() {
var MinusFive = this.age – 5;
return MinusFive
}
alert(a.age2)
hey Bucky I have a question why you are yearuntilRetire () … paranthesis .. will u please elaborate
Where the parameters added to the document.write statement because yearsUntilRetire is = to a function() ???
Seems like a weird attempt at implementing classes lol
A suggestion to anyone who watched this once (or twice). Try explaining it while you type out the code. After one go through, I emailed my dad and explained what each line meant. Why you use 'this.property', why you don't use parenthesis for 'yearsUntil' in the constructor etc. I had to sneak a peak a few times. Then watch the video again, taking notes. Finally, try doing it yourself without cheating. After the first watch through I was a bit lost, but 20 minutes later, 2 times watching it, and 3 times writing the code I feel more confident. Good luck everyone!
For
this.yearUntillRetire = yearsLeft;
you dont need a braces here because it is a DELEGATE. "yearsLeft" is called a delegate, you can think it as a Function Variable(variable that represents a function). It can also be passed around like variables and any delegate that it was assigned to can be used as a function and that could result in "yearsLeft"' function to be called
Dude I liked your Tutorial.. easier than reading a book. Which these days are becoming fatter and fatter.. like people. I didn't want to discourage you but official defining an object is mentioned as something like this. firstName, lastName are properties and fullName is a method
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
of course there are many ways to define an object
really could do with learning this shit but its so hard
I didn't understand why haven't you put parenthesis when you assign this.yeaUntilRetire= yearLeft and you put parenthesis when you call yearLeft in body. Can anyone plz help me .
I think you are a good teacher. Thank you for helping me understand this all better.
Fahrenheit, Kelvin… He forgot calculus XD
Thank you for not making this a monster. Simple well understandable language, I like it! Keep up 🙂
i thought functions didn't have scope with one another
Can i ask a question?
this.yearsUntilRetire = yearsLeft; // why there is not a bracket yearsLeft() .Is it mean that if we call a function without input any parameter ,so no need to put () ?
Wow now it piece of cake to learn the complexity data types behind function & objects thank Bucky you save me from a brain seizure 🙂
nice video
if the object's property is taking a function as it's value then that object property will become a method.
If you wanna write same code with Object Initializer:
<html>
<head>
<script type="text/javascript">
bucky = {name:"Bucky Roberts", age:24};
retireYear = {rYear:65};
</script>
</head>
<body>
<script type="text/javascript">
document.write(retireYear.rYear – bucky.age);
</script>
</body>
</html>
Y is he using two script tags ,one in head and one in body ,sorry for my ignorance
For the people who is suffering about passing parameter without instantiation into function(class looking methods lol), you have to initialize every passed parameter inside the function with a local variable, which is variable declared inside the function, otherwise Javascript does not recognize what you are trying to do with that parameter. The thing confuses me is that maybe I got that parameter to use in a function inside the class and I don't want to store it. Here is what I am trying to say :
function circle(radius,pi){
this.pi = pi; // if you don't declare "this.pi"(which clearly I don't want to
//waste memory by storing it)
this.radius = radius; // it does not compile.
this.area = calculateArea;
}
function calculateArea(){
return this.pi*this.radius*this.radius;
}
var object = new circle(5,3.14);
document.write(object.area());
Javascript syntax is so dumb…
pretty cool han!? 😀
Amazingly helpful! Super clear and easy to understand! Thanks.