2017-05-22 2 views
1

Ich bin also ein Anfänger hier und probiere aus, eine Web-App basierend auf einem Tutorial zu erstellen. Basierend auf dem, was ich gelernt habe, innerhalb eines Objekts kann ich die Eigenschaft des Objekts mit dem Code this.property-nameVerweis auf die Eigenschaft eines Objekts

Bezug auf die Codes unten arbeiten, nur meine Annahme bestätigen, dass 'this.todos 'wurde als Objekt unten über die Codes erstellt this.todos.push ({ todoText: todoText, abgeschlossen: false, });

und daher kann ich this.todos.property-name, wie this.todos.todoText und this.todos.completed irgendwo innerhalb toDoList Objekt?

Ich weiß nicht, wo ich das durcheinander mache. Vielen Dank, Leute.

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(); 
    } 

}; 

Antwort

0

Es gibt keine genaue Entsprechung zu Java getClass() oder Ruby .class in JavaScript. Meistens liegt das daran, dass JavaScript eine prototypbasierte Sprache ist, im Gegensatz zu Java, das klassenbasiert ist.

Je nachdem, was Sie brauchen getClass() für, gibt es mehrere Optionen in JavaScript:

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

Einige Beispiele:

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 

Kredit fällig: How to get a JavaScript object's class?

Verwandte Themen