John Cooper
John Cooper

Reputation: 7631

Couple of questions in backbone.js models

I am beginner in towards backbone.js. Here is what i am working towards.

http://jsfiddle.net/pW3SZ/

Person = Backbone.Model.extend({
        defaults: {
            name: 'Fetus',
            age: 0,
            children: []
        },
        initialize: function(){
            //alert("Welcome to this world");
        }
});

var person = new Person({name: 'John', age: '100'});
delete person;

//var person = new Person;
//person.set({name: 'Cooper', age: '90'});

alert(person.get('name') + ' ' + person.get('age'));
  1. I did delete my person instance, but when i alert why do i get John and 100 instead of default values.
  2. What is the difference between initialize and a constructor. Does it do the same job?
  3. In which part of memory does these value get stored. I know it could be stack, but can someone give an insight on this.

Upvotes: 0

Views: 300

Answers (2)

Igor Alekseev
Igor Alekseev

Reputation: 1198

  1. and 3. Really you didn't. delete remove property from object or element from array. You can't delete javascript object only remove pointers to this object and wait for GC to clean memory. Replace delete person; with person = undefined; to remove pointer

  2. Person.prototype.constructor is a real constructor where Backbone does it's own internal things. In the end it runs Person.prototype.initilalize -- place to put your on_create logic

Upvotes: 1

jakee
jakee

Reputation: 18556

-1 "The delete operator deletes an object, an object's property, or an element at a specified index in an array."

Basically as the delete operator works for arrays, if you had something like this fiddle. Check your js console, it throws an error, because the delete operator has removed the item from array.

-2 In Backbone.js you can treat the initialize -function like a constructor. You can use it to handle the parameters handed to the object at initialization in other that the default way.

For -3 I have no answer.

Hopefully this (partially) helped!

Upvotes: 1

Related Questions