2015-07-01 49 views
-1

我想了解学习JS之后的一段时间。我最近发现了一篇关于如何用sqlite和JS创建待办事项列表的简短教程。这里是一些代码:javascript/sqlite noob - 变量的声明

var html5rocks = {}; 
html5rocks.webdb = {}; 
html5rocks.webdb.db = null; 

html5rocks.webdb.open = function() { 
    var dbSize = 5 * 1024 * 1024; // 5MB 
    html5rocks.webdb.db = openDatabase("Todo", "1.0", "Todo manager", dbSize); 
} 

html5rocks.webdb.createTable = function() { 
    var db = html5rocks.webdb.db; 
    db.transaction(function(tx) { 
    tx.executeSql("CREATE TABLE IF NOT EXISTS todo(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME)", []); 
    }); 
} 

我不明白的是为什么html5rocks.webdb = {};html5rocks.webdb.db = null;'声明是这样写的那个。你怎么能指定webdbwebdb.db这种方式之前,你声明他们作为变数里面'html5rocks{}?

我从来没有见过这种方式来声明变量。 有人可以解释一下吗?

回答

0

{}是一个对象。

[]是一个数组。

所以,

var html5rocks = {}; // object html5rocks is made, and is still empty 
html5rocks.webdb = {}; // now html5rocks has a property: Webdb. Webdb itself is an object. 
html5rocks.webdb.db = null; // db, set to null, is added to Webdb (itself a property of html5rocks) 

关于JavaScript的好处:你不需要类,你就去做。你创造一个新的财产,你可以立即使用它。

OOP语言通常需要多行代码。在javascript中:existingObject.newProperty ='new value';

+0

谢谢你的回答。 – blender28