最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

closures - Javascript object literal, how to solve context? - Stack Overflow

programmeradmin1浏览0评论

I would like to start organizing my code properly, so I want to use object literals. In the following case, I'm doing a pseudo class. I would like that init() could work as a constructor, but unfortunately, I'm not seeing how to set attributes based on object context.

    var car = {
    context : this,
    wheels : 0,
    color : '',
    speed : 0,
    init : (function(x){
        console.log(x);
        x.wheels = 4;
        x.color = 'red';
        x.speed = 120;
    })(context)
};

console.log(car.color);

I would like to start organizing my code properly, so I want to use object literals. In the following case, I'm doing a pseudo class. I would like that init() could work as a constructor, but unfortunately, I'm not seeing how to set attributes based on object context.

    var car = {
    context : this,
    wheels : 0,
    color : '',
    speed : 0,
    init : (function(x){
        console.log(x);
        x.wheels = 4;
        x.color = 'red';
        x.speed = 120;
    })(context)
};

console.log(car.color);
Share Improve this question edited Jun 12, 2015 at 13:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 8, 2011 at 20:50 punkbitpunkbit 7,71710 gold badges57 silver badges95 bronze badges 2
  • What exactly you are trying to achieve? And how is it different from say: var car = new Car(); – c-smile Commented May 8, 2011 at 21:00
  • An Object object doesn't have a this property, only Function objects do. And in a function, this is a keyword that belongs to the activation object (effectively a variable), it is not a public property. Finally, a function's this is set by the call, you can't set it statically (though you can sort of using ES5 and bind). – RobG Commented May 8, 2011 at 23:16
Add a ment  | 

3 Answers 3

Reset to default 5

You can't immediately run a function like that whilst declaring an object literal. What you can do:

var car = {
init : function(wheels,color,speed){
    this.wheels = wheels || 0;
    this.color = color || '';
    this.speed = speed || 0;
    return this;
  }
}.init(4,'red',120);

alert(car.speed); //=>120

Which removes the need for:

context : this,
wheels : 0,
color : '',
speed : 0,

...and offers the possibility for:

var car = {
    init : function(wheels,color,speed){
      this.wheels = wheels || 0;
      this.color = color || '';
      this.speed = speed || 0;
      return this;
     }
    },
    redAndFast = car.init(4,'red',230),
    threeWheeler = car.init(3,'yellowstriped',110);

[edit] What was I thinking? If you want more instances of Car, you'll have to use a real constructor function instead of an object literal:

var Car = function(){
  return {
    init : function(wheels,color,speed){
            this.wheels = wheels || 0;
            this.color = color || '';
            this.speed = speed || 0;
            return this;
  }
 }
},
redAndFast = new Car().init(4,'red',230),
threeWheeler = new Car().init(3,'yellowstriped',110);

Which can be simplified to:

var Car = function(wheels,color,speed){
            this.wheels = wheels || 0;
            this.color = color || '';
            this.speed = speed || 0;
    },
    redAndFast = new Car(4,'red',230),
    threeWheeler = new Car(3,'yellowstriped',110);

Or if you wanted to cling on to some init like function:

var Car = (function(){
    function car(wheels,color,speed){
            this.wheels = wheels || 0;
            this.color = color || '';
            this.speed = speed || 0;
    }
    return {
        init: function(w,c,s){
            return new car(w,c,s);
        }
    };
 })(),
 redAndFast   = Car.init(4,'red',230),
 threeWheeler = Car.init(3,'yellowstriped',110);

But hey, what happened to my context? you may ask. Well, it turns out you didn't need it after all. Isn't javascript a beautiful and flexible language?

var Car = function() {
    this.wheels = 4;
    this.color = 'red';
    this.speed = 120;
}

var car = new Car();

It's best to use normal constructors for these kind of tasks.

Object literals work for singletons. If you want an instantiable object, you'll need to learn how js oop works and just use function objects.

发布评论

评论列表(0)

  1. 暂无评论