9951 explained code solutions for 126 technologies

backbone.jsHow do I use backbone.js in JavaScript?

Backbone.js is a JavaScript library that provides structure and organization to web applications by connecting models and views. It is a lightweight library that can be used to create single-page applications.

The following example shows how to use Backbone.js to create a model. The model is an object that contains the data and the methods to manipulate the data.

// Create a new Backbone model
var Person = Backbone.Model.extend({
  defaults: {
    name: 'John Doe',
    age: 30

  initialize: function() {
    console.log('A new person has been created');

// Instantiate a new model
var person = new Person();

Output example

A new person has been created

The code above does the following:

  1. var Person = Backbone.Model.extend({...}): Creates a new Backbone model called Person with two default properties - name and age.
  2. initialize: function() {...}: This is a function that will be called when a new instance of the model is created.
  3. var person = new Person(): This line creates an instance of the Person model.

For more information about Backbone.js, please refer to the Backbone.js documentation.

Edit this code on GitHub