Introduction to Objects I So What's a Method?


help me please
// here is bob again, with his usual properties
var bob = new Object(); = "Bob Smith";
bob.age = 30;
// this time we have added a method, setAge
bob.setAge = function (newAge){
bob.age = newAge;
// here we set bob's age to 40
// bob's feeling old. Use our method to set bob's age to 20




can you help me


You syntax error is caused by the last line of your code
which is a closing-curly-bracket-}


nice to know

You could test in labs
choose Javascript
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console


google search
== the Book ==
javascript [your question]

== discussions / opinions ==
javascript [your question]

== guidance == <---JavaScript code-convention


thanks for the help man I will come to you if I need to again and hopefully not the little things


function Person(name,age) { = name;
this.age = age;

// Let's make bob and susan again, using our constructor
var bob = new Person("Bob Smith", 30);
var susan = new Person("Susan Jordan", 25);
// help us make george, whose name is "George Washington" and age is 275
var george= new Object(); = "George Washington";
george.age = 275

Oops, try again. Make sure george was created using the Person constructor




function Person(name,age) { = name;
     this.age = age;

you have created a so-called Person Object class constructor

With the new keyword you are now able to create new object's
all having the same properties.

Thus with

var george = new Person("George Washington",275);

you create a new object with the same charateristics as bob and susan.
look at it with

console.log( george );



- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it’s associated value.

 var object1 = {
              name: "First"

So object1 has 1 property
a name property with property-key name and it’s associated string VALUE “FIRST”


var myObj = {
         type: 'fancy',
         disposition: 'sunny'

myObj has 2 properties seperated by a comma-,,
a type property with property-key type and an associated string VALUE 'fancy’
a disposition-property with property-key disposition and
…an associated string VALUE ‘sunny’.

= = = = = = = = = = = = = = = = = = = = = = =

To create an Object,
#you can use the literal notation,
you directly create an Instance of the object, with the
properties being separated by a comma-,

  var myObj = {
        type: 'fancy',
        disposition: 'sunny' 

#You create an Object by the construct notation.
First you create an empty Object by way of either
myObj = new Object(); or myObj = {};
and then you attach its properties using the syntax = it’s-associated-value ;
( = x ; )


  var myObj = {};
      myObj.type = 'fancy';
      myObj.disposition = 'sunny'; 

#There is also the facility Class construct notation.
The name should then start with a Capital-letter.

  var Person = function( theName, theAge ) { = theName;
         this.age = theAge;
         this.displayInstance = function() {
           console.log("The displayInstance -output-"+
                       "\n============================" +
                     "\n\t name: " + +
                     "\n\t age: " + this.age);
    //now create an Instance of this Class-object
  var myObj = new Person("Classy_Rocker",20);
    //call the Method displayInstance which takes NO parameters
  console.log( myObj );

you can run this in labs try it !!

As you can see i created a function within this constructor,
they now call this function a Method.
So if in near future the course is asking you to create a method you now know
that you have to create
a property-key with an associated value being a function within an Object.