6

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
Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
FatDogMark
  • 1,207
  • 2
  • 16
  • 25
  • 1
    `sheep` is already an object. I think reading about JavaScript basics would help you the most: [MDN JavaScript Guide](https://developer.mozilla.org/en-US/docs/JavaScript/Guide), especially [Working with Objects](https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects). – Felix Kling Dec 04 '12 at 06:42
  • @FatDogMark: What do you want exactly. sheep is already an object. – karthick Dec 04 '12 at 06:42
  • but how do i make another black sheep base on the sheep? to inherit the properties of sheep – FatDogMark Dec 04 '12 at 06: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. – ShinTakezou Dec 04 '12 at 06: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. – Felix Kling Dec 04 '12 at 06:44
  • @FelixKling thanks, I am not fresh at js, wanted just to scatter the idea of "copying" and it's why I made the comment and not the answer! :) (pointed in the edited comment too now) – ShinTakezou Dec 04 '12 at 06:46
  • Now you changed your code and answers / comments might become invalid... why did you do that? It really looks like you should read some introduction again, because the code is syntactically incorrect. – Felix Kling Dec 04 '12 at 06:53
  • though about having nothing to do with prototype: if you do a deep copy of an object and then modify the copy, you have used the original object de facto as a prototype; maybe it's not paradigmatic for js, but that's the concept of the prototype – ShinTakezou Dec 04 '12 at 06:57
  • @FelixKling are you addressing me? I've not changed the "code", just added the (this is not the real way you can do it in js but only the idea); if you've seen something different, it was just in your eyes. – ShinTakezou Dec 04 '12 at 07:00
  • @FatDogMark: I think you should look deeper into function expressions and function declarations. – karthick Dec 04 '12 at 07:07
  • @ShinTakezou: No, I was addressing the OP... and they changed their code again :-/ – Felix Kling Dec 04 '12 at 07:08
  • @FatDogMark: Please don't update your question with every iteration of your code. Most answers will become useless. Make comments on answers, try things and more importantly, read documentation! Blindly trying things, wondering why it does not work and then posting it here is not really fruitful. – Felix Kling Dec 04 '12 at 07:10
  • uhmm sorry i know, but i just think i missed something and edited it again,I just want to do thing like blacksheep = new sheep({colors:'black',sizes=10}); – FatDogMark Dec 04 '12 at 07:15

7 Answers7

3

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.

armen.shimoon
  • 6,303
  • 24
  • 32
1

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

var blacksheep = $.extend(sheep, { color: 'black' });
Richard Rout
  • 1,296
  • 1
  • 15
  • 28
  • I wouldn't do it this way without jQuery. Cloning objects is messy - an example with function Sheep() {...} should be used. Reference http://stackoverflow.com/questions/728360/copying-an-object-in-javascript for cloning javascript objects. – Richard Rout Dec 04 '12 at 06:50
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');

karthick
  • 11,998
  • 6
  • 56
  • 88
1
var sheep = function(){
    this.sizes = 100;
    this.eat = 100;
    this.colors = 'white';
    this.running = function(){
        return this.sizers + this.eat;
    }
}
karaxuna
  • 26,752
  • 13
  • 82
  • 117
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());
Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
  • 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?? – FatDogMark Dec 04 '12 at 07:11
  • 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: http://jsfiddle.net/SZQVn/ – Levi Botelho Dec 04 '12 at 07:14
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

Manibhadra
  • 400
  • 4
  • 6
-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");
Doink
  • 1,158
  • 6
  • 10
  • 1
    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. – Alok Swain Dec 04 '12 at 06:53