1

so i'm a beginner here and am trying out to creating a web app based on a tutorial. Based on what I've learned, within an object, I can reference the object's property by using the code this.property-name

Based on the codes below that work, just wish to confirm my assumption that 'this.todos' was created as an object below via the codes this.todos.push ({ todoText: todoText, completed: false, });

and hence, I can reference this.todos.property-name, like this.todos.todoText and this.todos.completed anywhere within toDoList object?

I don't know where I'm messing this up though. Thanks a bunch folks.

var toDoList = 
{
  todos: [],
  displayToDo: function()
  {
      if (this.todos.length ===0)
       {
        console.log ("your todos is empty")
        }
      else
        {
          console.log('My To Dos:');
          for (var i=0; i<this.todos.length; i++)
          {
            if (this.todos[i].completed === true)
            {
              console.log ('(x)', this.todos[i].todoText);
            }
            else
            {
              console.log ('( )', this.todos[i].todoText);
            }
         }
        }
  },

  addToDo: function(todoText)
  {
    this.todos.push
    ({
      todoText: todoText, 
      completed: false,
    });
    this.displayToDo();
  }, 


  changeToDo: function(position, todoText)
  {
    this.todos[position].todoText = todoText;
    this.displayToDo();
  }, 
  deleteToDo: function(position)
  {
    this.todos.splice(position, 1);
    this.displayToDo();
  },

  toggleCompleted: function(position)
  {
    var todo = this.todos[position];
    todo.completed = !todo.completed;
    this.displayToDo();
  },

  toggleAll: function()
  {
    var totalTodos = this.todos.length;
    var completedToDos = 0;


  for (var i=0; i<totalTodos; i++)
  {
    if (this.todos[i].completed === true)
    {
      completedToDos++;
    }
  }

  if (completedToDos === totalTodos)
      {
       for (var i=0; i<totalTodos; i++)
       {
       this.todos[i].completed = false;
       }
    }
    else
    {
       for (var i=0; i<totalTodos; i++)
       {
       this.todos[i].completed = true;
       }
    }
    this.displayToDo();
  }

};

1 Answers1

0

There's no exact counterpart to Java's getClass() or Ruby's .class in JavaScript. Mostly that's due to JavaScript being a prototype-based language, as opposed to Java being a class-based one.

Depending on what you need getClass() for, there are several options in JavaScript:

typeof
instanceof
obj.constructor
func.prototype, proto.isPrototypeOf

A few examples:

function Foo() {}
var foo = new Foo();

typeof Foo;             // == "function"
typeof foo;             // == "object"

foo instanceof Foo;     // == true
foo.constructor.name;   // == "Foo"
Foo.name                // == "Foo"    

Foo.prototype.isPrototypeOf(foo);   // == true

Foo.prototype.bar = function (x) {return x+x;};
foo.bar(21);            // == 42

Credit due: How to get a JavaScript object's class?

znon
  • 71
  • 2