2

I am trying to create variables inside object as follows but it is giving me error. Here is DEMO

And same code is below

var One = {
    var var1, var2;
    init: function () {
      var1 = 'some1';
      var2 = 'some2';
    }        
}

One.init();

Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • 1
    What are you trying to achieve? Should `var1` and `var2` be accessible outside of the `init` function? Why are they not properties of `One`? – James Allardice Nov 06 '12 at 11:09
  • I want these vars to be accessible inside `One` object but must not be accessible outside of it so that if I want to declare vars with the same name in other files should not create any conflicts – Om3ga Nov 06 '12 at 11:11
  • So why not declare them inside `init`? – James Allardice Nov 06 '12 at 11:11
  • I think that init function is mainly for initialization. Am I right? – Om3ga Nov 06 '12 at 11:13

3 Answers3

2

Objecs have properties not variables:

var One = {
    prop: "",
    prop2: "",
    init: function() {
       this.prop = 'some1';
       this.prop2 = 'some2';
    }        
}
Ram
  • 143,282
  • 16
  • 168
  • 197
  • Can I declare variables with the same name in other files? Will it create any conflicts with these one if I follow your procedure? – Om3ga Nov 06 '12 at 11:16
  • 1
    @x4f4r Yes, you can, there is no conflict between properties of an object and variables. – Ram Nov 06 '12 at 11:18
  • Thanks for helping me out but could you tell me that can I declare public and private properties using object literal? – Om3ga Nov 06 '12 at 12:07
  • @x4f4r The object-oriented features of JavaScript differ from other languages like PHP, there is no _private_ or _public_ keyword in JavaScript. – Ram Nov 06 '12 at 12:14
1

Object should look like this:

var Object = {

  /**
   * Documantation.
   */
  var1: null,

  /**
   * Documantation.
   */
  var2: null,

  /**
   * Documantation.
   */
  init: function () {
    this.var1 = 'some1';
    this.var2 = 'some1';
  }
};

Object.init();

But more information about the task to accomplish would be great, to be more clear with the answer.

Chango
  • 6,754
  • 1
  • 28
  • 37
  • Can I declare variables with the same name in other files? Will it create any conflicts with these one if I follow your procedure? – Om3ga Nov 06 '12 at 11:16
  • There concept of 'file' is no applicable in javascript, the variables are part of an object, so you will have conflicts only if you use the same object: `Object.var1 = "I'm changing var1 value";`. You should read more about javascript objects and scope. You have some information [Here](http://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-javascript-object-literal) and [here](http://www.quirksmode.org/js/this.html). – Chango Nov 06 '12 at 11:26
0

As an alternative, you can do this:

function One(Var1, Var2)
{
    this.var1 = Var1;
    this.var2 = Var2;
}

var OneExample = new One('some1','some2');

var OneExampleVar1 = OneExample.var1;

EDIT - Here is a fiddle demo

Pete Uh
  • 630
  • 4
  • 7