5 Replies - 1211 Views - Last Post: 05 December 2013 - 11:27 PM

#1 HeeJo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-July 13

How to create methods in javascript

Posted 05 December 2013 - 11:18 AM

Hi Everyone,

I tried searching on google looking for tutorials on how to create methods using objects, but I can't seem to find the ones that work.

	function get_payment() {
		var the_payment=250;
		the_payment += (this.seats=="leather") ? 100 : 50;
		return the_payment;
	}

	function car(seats, engine, transmission) {
		this.seats=seats;
		this.engine=engine;
		this.transmission=transmission;
		this.payment = get_payment;
	}
		
	newCar={ seats:"leather", engine:"vtec", transmission:"manual" };
	var newCar_Payment = newCar.payment();
        document.write("This new car has " + newCar.seats + " seats with " + newCar.engine + " engine and is " +  newCar.transmission);
	document.write("<br>Payment: " + newCar_Payment);



So far I've been working on this for a while, and whenever I declare the variable assigning to the method get_payment(), everything seizes to work. If anyone could teach me how I can create and declare methods the right way, would be of a great help.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to create methods in javascript

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4184
  • View blog
  • Posts: 13,228
  • Joined: 08-June 10

Re: How to create methods in javascript

Posted 05 December 2013 - 01:41 PM

this whole code doesn’t make any sense.

you should definitely find a recent tutorial about creating custom objects in JS.

e.g.
- https://developer.mo...nted_Javascript
- https://developer.mo...prototype_chain
- https://developer.mo...tance_Revisited

PS. document.write() will definitely do more harm than good in this environment.
Was This Post Helpful? 2
  • +
  • -

#3 HeeJo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-July 13

Re: How to create methods in javascript

Posted 05 December 2013 - 08:36 PM

Thanks for the reply, Dormilich.
I'll definitely have a look at the links you provided. So far it looks pretty good, better than the textbook tutorials I've been following. I just started learning javascript and I absolutely love it but the downside is, debugging is a pain in the ass

This post has been edited by Dormilich: 05 December 2013 - 10:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4184
  • View blog
  • Posts: 13,228
  • Joined: 08-June 10

Re: How to create methods in javascript

Posted 05 December 2013 - 10:11 PM

Quote

I just started learning javascript and I absolutely love it but the downside is, debugging is a pain in the ass

why that? almost every browser comes with sensible development tools (and at least an Error Console).
Was This Post Helpful? 0
  • +
  • -

#5 HeeJo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-July 13

Re: How to create methods in javascript

Posted 05 December 2013 - 10:45 PM

View PostDormilich, on 05 December 2013 - 10:11 PM, said:

why that? almost every browser comes with sensible development tools (and at least an Error Console).


Oh, What I really meant was javascript isn't a strongly typed language, which is very error prone, hence harder to debug. I learned a bit of C# and Java before learning javascript, they do share a lot commonalities but I'm fairly new with scripting languages such as javascript even though it's an object oriented language. Still got a lot to learn.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4184
  • View blog
  • Posts: 13,228
  • Joined: 08-June 10

Re: How to create methods in javascript

Posted 05 December 2013 - 11:27 PM

View PostHeeJo, on 06 December 2013 - 06:45 AM, said:

Oh, What I really meant was javascript isn't a strongly typed language, which is very error prone, hence harder to debug.

that’s just a matter of training. you could as well say that a strongly typed language is harder to debug because you need to keep your variables’ data type in mind.

and I wouldn’t say that a weakly typed language is more error prone per se, otherwise they wouldn’t have been developed (or worked out as they are).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1