Difference between toJSON() and JSON.Stringify()

前端 未结 3 1190
傲寒
傲寒 2020-12-02 13:16

if you need to read or clone all of a model’s data attributes, use its toJSON() method. This method returns a copy of the attributes as an object (not a J

3条回答
  •  死守一世寂寞
    2020-12-02 13:46

    I'm also reading Addy Osmani's Developing backbone.js application, and I have the same question. I figured out by trying his example (the todo list) in the console.

    var Todo = Backbone.Model.extend({
        defaults:{
             title:"",
             completed:false
    }
    });
    
    var todo1 = new Todo(); 
    
    
    console.log(todo1.toJSON())
    //The console shows
    //Object {title: "finish your assignment", completed: false}
    
    console.log(JSON.stringify(todo1))
    //The console shows
    //{"title":"finish your assignment","completed":false}
    

提交回复
热议问题