function house (windows,doors,attic) {
this.windows = windows;
this.doors = wood;
this.attic = stuff;
}
var parts = house_two ("glass","wood","spiders");
var house_parts = house_two.windows;

document.write("I want" +house_parts+ "doors");

I want to understand objects in javascript, so far it hasn't been that exciting as learning other parts of the language. I want to know what is wrong with the code above, and what is right ?
I know I can use object literals I don't want to use literals just yet.

Recommended Answers

All 40 Replies

I think you want it to be something like this:

function House (w, d, a) {
    //i trimmed down the passed variables for clarity as to what goes where
    this.windows = w;
    this.doors = d;
    this.attic = a;
}

//create an instance of House passing in the variables
var myHouse = new House("glass","wood","spiders"),
    house_parts = myHouse.windows;

document.write("I want " + house_parts + " doors!");

This will output "I want glass doors!"

The word "this" I know must always go infront. Must objects be created in functions ? Also your instance is put within a variable, in your example "new house" there is no hard rule for this, other then the word "new" must be placed before, correct ?

The word "this" I know must always go infront. Must objects be created in functions ?

In javascript you don't have classes, so if you check your console and you simply pass an array from php to javascript you'll have an object with properties. So basically when you declare a function that has variables associated with it (with the word "this") they become the object's properties.

The word "this" I know must always go infront. Must objects be created in functions ?

When you create an instance of an object you need to call it a name so you can use it in the rest of the code, that's why you instantiate it by assigning it to a variable. In spoken English it would be the same as saying myHouse is going to be my new House, then house_parts is going to contain myHouse's windows. Now I can simply use myHouse to access any part of it.

You could easily not create the variable house_parts and simply write document.write("I want " + myHouse.windows + " doors!"); and that would be more readable when going back to that script.

You should also look into javascript's prototype object where you can add more stuff to it and make it behave like a regular OOP class with polymorfism and such.

In javascript you don't have classes, so if you check your console and you simply pass an array from php to javascript you'll have an object with properties. So basically when you declare a function that has variables associated with it (with the word "this") they become the object's properties.

Sorry I don't understand ? What if I don't put objects in a function is that even possible ?

When you create an instance of an object you need to call it a name so you can use it in the rest of the code, that's why you instantiate it by assigning it to a variable. In spoken English it would be the same as saying myHouse is going to be my new House, then house_parts is going to contain myHouse's windows. Now I can simply use myHouse to access any part of it.

You could easily not create the variable house_parts and simply write document.write("I want " + myHouse.windows + " doors!"); and that would be more readable when going back to that script.

You are placing the function house() in a variable but since it's an instance you have to put "new" infront. Can you call the instance before the function ?

var myHouse = new House("glass","wood","spiders"),

I don't understand your questions, can you give me an example? There are several ways of doing it, but I don't know how you want to do it or why.

Have a read here to see if you can clear this up for me.

function house (windows,doors,attic) {
this.windows = windows;
this.doors = wood;
this.attic = stuff;
}
var parts = new house_two ("glass","wood","spiders");
var sections = parts.doors;
//var house_parts = house_two.windows;

document.write("I want" +sections+ "doors");

First you can't write var parts = new house_two() because you didn't create the function house_two(). I'm going to try and guess what you're asking though.

You can create several houses like so:

var house_one, house_two, house_three;
house_one = new House("glass","wood","spiders");
house_two = new House("water","sand","fire");
house_three = new House("foo","bar","baz");

Now you can access each house through their names(var names).

Try using some of the objects javascript already has, instead of thinking about making one just yet.

var date_one, date_two, date_three;
date_one = new Date(2001, 01, 01, 01, 01, 01, 00);
date_two = new Date(2002, 02, 02, 02, 02, 02, 00);
date_three = new Date(2003, 03, 03, 03, 03, 03, 00);

Now you have 3 different dates that can all be accessed by their names date_one, date_two, date_three.

If you had done something like this:

var date_one, date_two, date_three;
date_one = new Date_one(2001, 01, 01, 01, 01, 01, 00);
date_two = new Date_two(2002, 02, 02, 02, 02, 02, 00);
date_three = new Date_three(2003, 03, 03, 03, 03, 03, 00);

Javascript would tell you functions Date_one(), Date_two(), Date_three() weren't created yet and would throw an error.

The first is to put your local variables in a function, give the function a name then place names in the instance.

function the_house() {
      var house_one, house_two, house_three;
       house_one = new House("glass","wood","spiders"); // Names in instance
        house_two = new House("water","sand","fire");
        house_three = new House("foo","bar","baz");
}

I understand how to create the instance, but what would you call the items in the brackets, the parameters, of the instance ?

var house_one, house_two, house_three;
house_one = new House("glass","wood","spiders");
house_two = new House("water","sand","fire");
house_three = new House("foo","bar","baz");

Yes.

On this line house_one = new House("glass","wood","spiders"); you have the following:

house_one is the variable that was assigned the object's instance;
new House() is how you create an instance of an object (generally, not exclusively);
"glass","wood","spiders" are the object's properties (which can be considered as being the function's parameters, but in OOP and belonging to an object they are generally called properties);

On your last reply, the first block of code would basically run a function that would create 3 houses, but I'm guessing they won't be accessible from outside the function since you didn't declare them as this.house_one... So they would work only within the function the_house();

I hope I'm helping, I really can't see what you are asking.

You are helping, a common ground will be found soon :)

I created instances, but by not using "this" I can't use them outside of the function, correct ?

Can the function as the code block currently is, be used for anything or it's a pointless block of code ?

I thought javascript wasn't a OOP language ?

function the_house() {
var house_one, house_two, house_three;
house_one = new House("glass","wood","spiders"); // Names in instance
house_two = new House("water","sand","fire");
house_three = new House("foo","bar","baz");
}

Q1 - that is correct.

Q2 - the block as it is, is useless. You would either have to continue
developing the_house() function to produce output directly from it, like adding console.log(house_two). That would make it have some functionality. But no, as it is, it doesn't produce anything.

Q3 - Javascript isn't OOP in the sense it doesn't have classes, period. Having that said, it is very OOP in the sense you can use the prototype object to provide inheritance, polymorphism, coupling... and all those big words that follow OOP.

Helpful tips, the function otherwise is good, but incomplete ? I've done a little reading on objects, up to this point it's beginning to make more sense, that is what I want, except more :)

How would the function be complete, by adding this (is there another term I can refer to "this" as other then "this" ?) outside of the function, then instancing, as in my previous example, house_two ?

Imagine you only need a few bits of data from a user and the function(object) does the rest. This is just a short example, take a look here at the output.

function Price(basePrice) {
    this.discount = 0.85;
    this.base = basePrice;
    var tax = 1.23;
    this.result = this.base * this.discount * tax;
}

var myPrice = new Price(10);
console.log(myPrice); //output the object
console.log(myPrice.discount); //a property
console.log(myPrice.base); //another property
console.log(myPrice.tax); //undefined (out of scope)
console.log(myPrice.result); //final property

You can also have a look at this where you can create an object and then call a function to manipulate that object. You can read the whole thing, there's a lot of cool stuff there, but take a peek at the chapter "Defining methods".

By the way there is no such thing as a complete function... someone always wants more, lol xD

You defined any object in the function:
this.discount = 0.85;
var tax = 1.23;

Then you definied the instance with myPrice. If I'm understanding everything correctly ?

Followed by, you gave the function a value in a variable as a instance with newPrice(10) which gets passed to the function ?

console.log(myPrice.tax); //undefined (out of scope)
(How is this out of scope, it's 1.23 ?)

You defined any object in the function

I defined the object's properties this.discount = 0.85; var tax = 1.23;
Where discount is public (accessible from outside the object) and tax is private (only accessible from within the object).

Then you definied the instance with myPrice.

myPrice is the variable that has the Price object assigned to it.

you gave the function a value in a variable as a instance with new Price(10) which gets passed to the function

If you check the function again you will see Price(basePrice) and inside this.base = basePrice. So when I do new Price(10) I'm saying that the parameter basePrice = 10, so yes that value is passed into the object, specifically to the base property of the object.

How is this out of scope, it's 1.23 ?

That property is out of scope because I tried to access it from outside the object definition (the "function"). Because I didn't say this.tax = 1.23; it means that value will be only inside the object and I cannot use it from outside. That is what's called a private property, that nobody is supposed to change.

When using the word "this" you can access it outside of the function or the object ? Why would you call it a object when it's a function, because it has objects within ?

myPrice is the variable that has the Price object assigned to it.

Isn't the Price object a instance or it's not a instance because "this" wasn't asssigned in the function / object ?

If you check the function again you will see Price(basePrice) and inside this.base = basePrice. So when I do new Price(10) I'm saying that the parameter basePrice = 10, so yes that value is passed into the object, specifically to the base property of the object.

In other words, you sent newPrice(10), in this case the value of 10 to Price(basePrice) ? Then again you have the object this.base=basePrice.

That property is out of scope because I tried to access it from outside the object definition (the "function"). Because I didn't say this.tax = 1.23; it means that value will be only inside the object and I cannot use it from outside. That is what's called a private property, that nobody is supposed to change.

I assume because you did not create an object called myPrice ?

What is the difference between an OOB (acromyn) language or term and a DOM system that is javascript ?

function butel (food,items,candy) {
    this.food = "bread";
    this.items = "rocking_chair";
    this.candy = "candycorn";
}
var results = new butel ("vineger","cabnet","milkduds");
var pt = results.food;
document.write(pt);

What about situation such as this, I have my object, but then my instance also has parameters, but the write command is reading the object properties, not the instances ?

You can create a function like this:

function name(parameter) {
    var var1, var2, var3, result;
    var1 = parameter;
    var2 = 2;
    var3 = 3;

    result = var1 + var2 + var3;
    return result;
}

var a = name(1);
console.log(a); //outputs number 6

var b = new name(1);
console.log(b); //outputs an object with no value

This is a function that adds 3 values, one of which is passed through from the "outside". This function was created to perform a certain task and not to act as an object, it just exists to return the sum of those three values.
a is a variable with the value returned from the function
b is a variable with the instantiated object name() but it's behaviour isn't OO

function name(parameter) {
    this.var1 = parameter;
    this.var2 = 2;
    this.var3 = 3;
    this.result = var1 + var2 + var3;

}

var a = name(1);
console.log(a); //outputs undefined

var b = new name(1);
console.log(b); //outputs an object
console.log(b.result); //outputs number 6

/*
this is what is output by console.log(b);
[object Object] {
  result: 6,
  var1: 1,
  var2: 2,
  var3: 3
}
*/

In the second case:
a is a variable with the value undefined because we gave the "function" an object behaviour and not just a simple task to return a value.
b is a variable with the instantiated object name() and this way the "function" has OO behaviour.

I assume because you did not create an object called myPrice?

I created a variable called myPrice and instantiated an object of type Price.

What is the difference between an OOB (acromyn) language or term and a DOM system that is javascript?

Do you mean OOP (Object Oriented Programming)?
In an OOP language you can create a class class Person {} and give it all the behaviour you want, but have another class inherit from it directly class Woman inherits Person {} meaning the object Woman is a type of Person and then you could make class Man inherits Person {} as Man is also a type of Person. So now a person can use the app, but a Woman will have different behaviour then a Man. But as a Person they would share the parent class' behaviour.
In a DOM based system it's a bit different, but not so straight forward. You'd need to use the prototype object to do the same thing.

function name(parameter) {
var var1, var2, var3, result;
var1 = parameter;
var2 = 2;
var3 = 3;
result = var1 + var2 + var3;
return result;
}
var a = name(1);
console.log(a); //outputs number 6
var b = new name(1);
console.log(b); //outputs an object with no value

Variable a doesn't have a value assigned, how did you get an output number of 6 ?

Use JS BIN or JSFiddle to test the code, I suggest JS BIN since you can actually see the console without going into dev-tools.

The function name() will return result; since result = 6, that will be the value of a. Not the same when it's the object example.

How are you getting 6, when 2 and 3 equals 5 ?

Because var1 = 1;... the variable passed within () in this case parameter will be = to the variable parameter within the function.

I can't make arrows here to explain, but look at every variable name I'm placing withing () and where they are in the function.

function example(a, b, c){
    var var1 = a,
        var2 = b,
        var3 = c,
        result = a + b + c;
    return result;
}
//this function will do exactly the same as "example();"
function sameAsExample(){
    var result;
    return result = a + b + c;
}
var test1 = example(1, 2, 3), // = 6
    test2 = example(2, 4, 6), // = 12
    test3 = example(3, 5, 2); // = 10

When you understand this, take a look back on this discussion to see if things make more sense to you.

If you sent the following: test2 = example(2, 4, 6),
The function wuld output 12, correct ? In your previous examples you sent test2 = example(1,2,3) which gave 6 !

You are correct test2 = example(2, 4, 6) = 12.

And I just saw that I forgot to add this to my last post, had a mistake there

function sameAsExample(a, b, c){//mistake was here
    var result;
    return result = a + b + c;
}
function something(chair //1,vinager //2,laptop //3) //writes in order  {
this.chair = chair; //1
this.food = vinager; //2
this.laptop = laptop; //3
}

If I change this.food to this.foody or vineger to vine and I write out the object it writes the object food, why ? I hope that made sense ? :)

Do you mean going from this:

function something(chair, vinager, laptop) {
    this.chair = chair; //1
    this.food = vinager; //2
    this.laptop = laptop; //3
}

To this?

function something(chair, vine, laptop) {
    this.chair = chair; //1
    this.foody = vine; //2
    this.laptop = laptop; //3
}

Try pasting the whole block you're using so I can replicate it.

The other part of the block that I didn't post in the message; was simply writing out one of the objects. The second block of code in your message Fernado is what I mean't.

Second Block of code from the message :)

function something(chair, vine, laptop) {
this.chair = chair; //1
this.foody = vine; //2
this.laptop = laptop; //3
}

I was working on the following, scratching my head as to why it wouldn't write out "pepsi" I don't see nothing wrong with the object, although there is :)

function grow(white,brown,yellow) {
this white=white;
this brown=brown;
this yellow=yellow;
}
var iol = new grow("something","gook","pepsi");
var how = iol.yellow;
document.write("I want to " +how+ " sure");

You need to add . between this and the property name, like so:

function grow(white, brown, yellow) {
  this.white=white;
  this.brown=brown;
  this.yellow=yellow;
}
var iol = new grow("something", "gook", "pepsi"),
    how = iol.yellow;
document.write("I want to " +how+ " sure");

But you don't need to create the variable how, try this:

var iol = new grow("something", "gook", "pepsi");
document.write("I want to " + iol.yellow + " sure");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.