[英]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
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: 看看jQuery的extend
功能:
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. 这会将sheep
所有属性复制到blacksheep
,然后将第三个参数合并为blacksheep
,从而有效地实现您想要的效果。
要制作另一只基于绵羊的黑羊,在这种情况下你可以做(使用jQuery):
var blacksheep = $.extend(sheep, { color: 'black' });
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'); var sheep1 = new Sheep('100','100','white');
var sheep = function(){
this.sizes = 100;
this.eat = 100;
this.colors = 'white';
this.running = function(){
return this.sizers + this.eat;
}
}
You don't declare objects in JavaScript in the same way as you do in strongly-typed languages. 您不能像在强类型语言中那样在JavaScript中声明对象。 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
的子对象的新对象。 options
contains all of your methods. options
包含您的所有方法。 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());
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; var sheep = {sizes:100,eat:100,colors:'white',running:function(){return this.sizes + this.eat; } }; }}; Object.defineProperty(sheep, 'colors', { value: 'black' , writable: true }); Object.defineProperty(sheep,'colors',{value:'black',writable:true});
Thanks 谢谢
Javascript is prototype based not class based. Javascript是基于原型而不是基于类的。 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.