javascript - Create an object with properties, - Stack Overflow

I am new to javascript...I trying to create an object- "Flower".Every Flower has it propert

I am new to javascript... I trying to create an object- "Flower". Every Flower has it properties: price,color,height...

Can somebody give me an idea how to build it?

Create an object and then change his properties?

:-)

I am new to javascript... I trying to create an object- "Flower". Every Flower has it properties: price,color,height...

Can somebody give me an idea how to build it?

Create an object and then change his properties?

:-)

Share Improve this question edited Nov 22, 2011 at 9:44 Richard Dalton 35.8k6 gold badges74 silver badges92 bronze badges asked Nov 22, 2011 at 9:39 BorisDBorisD 2,2615 gold badges25 silver badges35 bronze badges 1
  • A normal JavaScript object? You won't need jQuery for that – Pekka Commented Nov 22, 2011 at 9:41
Add a comment  | 

5 Answers 5

Reset to default 22
flower= {
 price : function() { 
     console.log('Price is 78 $'); 
 },
 color: 'red',
 height : 23
};

flower.price();
flower.height ;

Have an object, where you can also bind functions to. The following should be used if you want to have multiple Flower objects, because you can easily create new Flowers and they will all have the functions you have added:

function Flower(price, color, height){
    this.price = price;
    this.color= color;
    this.height= height;

    this.myfunction = function()
    {
        alert(this.color);
    }
}

var fl = new Flower(12, "green", 65);
fl.color = "new color");

alert(fl.color);
fl.myfunction();

If you want to have a sort of array just use an object literal, but you need to set the properties and functions for each Object you create.

var flower = { price : 12, 
               color : "green",
               myfunction : function(){
                   alert(this.price);
               }
};
flower.price = 20;
alert(flower.price);
alert(flower.myfunction());
var flower = {"height" : 18.3, "price":10.0, "color":"blue"}

Here is a pattern to create object with public/private section(s)

var MyObj = function()
{
    // private section
    var privateColor = 'red';

    function privateMethod()
    {
        console.log('privateMethod. The color is: ', privateColor);
    }

    // The public section
    return
    {
        publicColor : 'blue',
        publicMehtod: function()
        {
            // See the diffrent usage to 'this' keyword
            console.log('publicMehtod. publicColor:', this.publicColor, ', Private color: ', privateColor);
        },
        setPrivateColor: function(newColor)
        {
            // No need for this
            privateColor = newColor;
        },
        debug: function()
        {
            this.publicMehtod();
        }
    };
}

var obj1 = new MyObj();
obj1.publicMehtod();
obj1.setPrivateColor('Yellow');
obj1.publicMehtod();

var obj2 = new MyObj();
obj2.publicMehtod();
var flower = {"propertyName1": propertyValue1, "propertyName2": propertyValue}; 

To retrieve the values:

var price = flower.price;

To change property values:

flower.price = newPrice;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1737524714a3954678.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信