What is the best way to create a JavaScript object so that it works? And passes JSLint?

I am trying to check if I can build JavaScript objects as intuitively as possible, making sure that it is “correct” as much as possible. I ran a bunch of different scripts through Crockford JSLint.com and didn't have much luck. It seems I fixed one error, then another pops up due to a change. The following is about as good as I can get. Anyone else take on this?

This is a typical way to structure an object:

function gizmo(id) {

  /* private variables */

  var myId = id;

  /* private methods */

  var init = function () {
    if (myId < 1) {
      setId(1);
    }
  };

  var setId = function (newId) {
    myId = newId;
  };

  // run 'constructor'
  init();

  /* public methods */

  return {
    getId: function () {
      return myId;
    },
    setId: function (newId) {
      setId(newId);
    },
    incrementId: function (inc) {
      setId(myId + inc);
    }
  };
}

// creating an instance of gizmo

var myGizmo = gizmo(-2);
console.log(myGizmo.getId()); // outputs 1

myGizmo.setId(5);
console.log(myGizmo.getId()); // outputs 5

myGizmo.incrementId(2);
console.log(myGizmo.getId()); /// outputs 7

This seems to work well. However, when I run it through JSLint, it gives me an error stating that my two private functions are "Implied Global".

The best I can come up with is to declare my functions on top with the following variables:

function gizmo(id) {

  /* private variables */

  var myId = id,
      init,
      setId;

  /* private methods */

  init = function () {
    if (myId < 1) {
      setId(1);
    }
  };

  setId = function (newId) {
    myId = newId;
  };

  // run 'constructor'
  init();

  /* public methods */

  return {
    getId: function () {
      return myId;
    },
    setId: function (newId) {
      setId(newId);
    },
    incrementId: function (inc) {
      setId(myId + inc);
    }
  };
}
+5
4

, JSLint. setId, , . , var , ECMAScript 5 10.5. , . , undefined. , (, init) , undefined.

, setId undefined, , :

function setId()
{
  alert("Global setId");
}
function f()
{
  var init = function()
  {
    setId();
  }
  alert(typeof(setId));
  init();
  var setId = function()
  {

  }
}

undefined, TypeError.

+1

JSLint , setId , init.

JSLint.

function gizmo(id) {

  /* private variables */

  var myId = id;

  /* private methods */

  var setId = function (newId) {
    myId = newId;
  };

  var init = function () {
    if (myId < 1) {
      setId(1);
    }
  };

  // run 'constructor'
  init();

  /* public methods */

  return {
    getId: function () {
      return myId;
    },
    setId: function (newId) {
      setId(newId);
    },
    incrementId: function (inc) {
      setId(myId + inc);
    }
  };
}
+3

JSlint, "javascript " .

:

Mogwai={
  has_crazy_thoughts:True,
  reacts_to_water:True,
  name: 'Gizmo',
  eat:function(food){
    // code
  },
  become_gremlin:function(){
    // code
  },
  cause_havoc:function(){
    // code
  }
}

. . Javascript - Java.

EDIT: , ( ): http://oreilly.com/catalog/9780596517748... , JSlint.

0

( ), JavaScript, . , , , .

First off, the way you create your objects seems very much like a Module Pattern . As far as I remember, the article to which I am attached read very well about this. Secondly, perhaps you would prefer a different way to instantiate objects . In this article, you are a little different.

0
source

All Articles