5

Why is this not working??

    var sheep = function(options){
        this.options = {sizes: 100,
                        eat: 100,
                 colors: 'white', 
                 running: function () {
                     return this.sizes + this.eat;
                 }
          }
    };

    var blacksheep = new sheep({colors:'black'});       

    alert('blackcsheep color is ' + blacksheep.colors);//error undefined
    alert('blackcsheep color is ' + blacksheep.options.colors);// it return white
    alert('blackcsheep running is ' + blacksheep.running());//error
14
  • 1
    sheep is already an object. I think reading about JavaScript basics would help you the most: MDN JavaScript Guide, especially Working with Objects. Commented Dec 4, 2012 at 6:42
  • @FatDogMark: What do you want exactly. sheep is already an object. Commented Dec 4, 2012 at 6:42
  • but how do i make another black sheep base on the sheep? to inherit the properties of sheep Commented Dec 4, 2012 at 6:42
  • js is a prototype based language, so to say, so that your sheep can't be used as a class. you rather copy the sheep (this is not the real way you can do it in js but only the idea): blacksheep = sheep; blacksheep.color = 'black'; and you are done more or less. Commented Dec 4, 2012 at 6:43
  • 3
    @ShinTakezou: blacksheep = sheep would not create a copy of sheep, both variables just refer to the same object. This has nothing to do with prototypal inheritance. Commented Dec 4, 2012 at 6:44

7 Answers 7

2

The syntax:

var sheep = {sizes:100, eat:100, colors:'white',running:function(){
        return this.sizes+this.eat;
        }
    };

is an object literal. It defines an instance of an object, but not the class that defines it. Therefore, there is no way to "new-up" another instance of the object.

Take a look at jQuery's extend functionality:

var blacksheep = {
}

$.extend(blacksheep, sheep, { color: black });

This will copy all the properties of sheep into blacksheep, then merge the third parameter into blacksheep, effectively achieving what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

$.extend is just convenience for me, alright I accept the answer
1

To make another black sheep based on sheep, in this scenario you could do (using jQuery):

var blacksheep = $.extend(sheep, { color: 'black' });

1 Comment

I wouldn't do it this way without jQuery. Cloning objects is messy - an example with function Sheep() {...} should be used. Reference stackoverflow.com/questions/728360/… for cloning javascript objects.
1

You can create a sheep object like this.

 function Sheep(sizes,eat,colors){
    this.sizes = sizes;
    this.eat = eat;
    this.colors = colors;
    this.running = function (){
     return this.sizes+this.eat;
    }

    }

Alternatively you can write like this also

 function Sheep(sizes,eat,colors){
    this.sizes = sizes;
    this.eat = eat;
    this.colors = colors;        
    }
 sheep.prototype.running = function(){
 return this.sizes + this.eat;    
}

var sheep1 = new Sheep('100','100','white');

Comments

1
var sheep = function(){
    this.sizes = 100;
    this.eat = 100;
    this.colors = 'white';
    this.running = function(){
        return this.sizers + this.eat;
    }
}

3 Comments

why would I need 'this' in all variables ... can't I just wrap my sheep object in a function and turn it into class?
var a = new sheep(); a now has property sizes... because of 'this'
'this' is current sheep class. Read here what does it mean: quirksmode.org/js/this.html
1

You don't declare objects in JavaScript in the same way as you do in strongly-typed languages. You declare objects by using functions like this:

function sheep() {
    this.color = "white";
    this.size = 200;
    this.speed = 100;
    this.running = function () {
        return "the sheep is running!";
    };
}

var blacksheep = new sheep();

alert('sheep size is ' + blacksheep.size);
alert('sheep running is ' + blacksheep.running());​

Your new object does not work because you are creating a new object with a sub-object called options. options contains all of your methods. As such only the second of these three lines that you gave will give you the correct response:

alert('blackcsheep color is ' + blacksheep.colors);
alert('blackcsheep color is ' + blacksheep.options.colors); // Only this one correctly references `options`.
alert('blackcsheep running is ' + blacksheep.running());

2 Comments

I see some people make class dont need to this everything that put this.options{...} then he can create new object like blacksheep = new sheep({colors:'black',sizes=10}); , how to do that??
My first example shows how to do just that. When you call the function new sheep(), this refers to the new object that is being created. What var blacksheep = new sheep() does is apply all the properties referenced by this in the sheep() function to the variable blacksheep. As such you can then go on to reference as I did, blacksheep.size, blacksheep.running() and so forth. Try this out here: jsfiddle.net/SZQVn
0

in your case sheep is already an object you can not create object of an object. you can directly use that object with property.

But i think you want something like this

var sheep = {sizes:100, eat:100, colors:'white', running:function(){ return this.sizes+this.eat; } }; Object.defineProperty(sheep, 'colors', { value: 'black' , writable: true });

Thanks

Comments

-1

Javascript is prototype based not class based. It does not use classes and is also object orientated.

var whitesheep =new sheep("100","100","white","running");
var blacksheep =new sheep("100","100","black","running");

1 Comment

Did you try this out in browser console ? this will not work unless you define a function object and returns some kind of object which has its properties as the values passed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.