javascript/sqlite noob-变量声明
在学习了一段时间JS之后,我正在尝试学习sqlite。我最近发现了一个关于如何使用sqlite和JS创建待办事项列表的简短教程。下面是一些代码:javascript/sqlite noob-变量声明,javascript,sqlite,Javascript,Sqlite,在学习了一段时间JS之后,我正在尝试学习sqlite。我最近发现了一个关于如何使用sqlite和JS创建待办事项列表的简短教程。下面是一些代码: var html5rocks = {}; html5rocks.webdb = {}; html5rocks.webdb.db = null; html5rocks.webdb.open = function() { var dbSize = 5 * 1024 * 1024; // 5MB html5rocks.webdb.db = open
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;'代码>就是这样写的。在将它们声明为'html5rocks{}中的变量之前,如何以这种方式分配webdb
和webdb.db
我从未见过这种声明变量的方法。
有人能解释一下吗 {}是一个对象
[]是一个数组
所以
javascript的好处是:你不需要类,你只需要这样做。如果你创建了一个新的属性,你可以立即使用它
OOP语言通常需要多行代码。在javascript中:existingObject.newProperty='newvalue'
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 )