How to create a new object in JavaScript?

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 
+6
source share
7 answers

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. Thus, there is no way to β€œupdate” another instance of the object.

Take a look at jQuery extend functionality:

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

This will copy all the properties of the sheep into blacksheep , and then combine the third parameter into blacksheep , effectively doing what you want.

+3
source

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

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

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 also write

  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');

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

You do not declare objects in JavaScript in the same way as in strongly typed languages. You declare objects using the following functions:

 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 your methods. Thus, only the second of these three lines that you gave will give you the correct answer:

 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()); 
+1
source

in your case, the sheep is already an object that cannot be created as an object of an object. you can directly use this object with a property.

But I think you want something like this

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

thanks

0
source

Javascript is a non-class based prototype. It does not use classes and is also object oriented.

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

All Articles