javascript中有三种原始类型:布尔型、数值型(不区分浮点数和整数)和字符串型。
此外,还有对象类型和包含可执行代码的函数类型。前者是一种复合类型(数组是一种特殊的对象)。
最后,还有空类型(null)和未定义类型(undefined)。
原始数据类型按值传送,其他数据类型则按引用传送。
1 2 3 4 5 | (function(){ var foo = 10; var bar = 2; alert(foo * bar); })(); |
---|
1 2 3 4 5 6 7 8 9 | var baz; (function(){ var foo = 10; var bar = 2; baz = function(){ return foo * bar; } })(); baz(); //baz可以访问foo和bar,即使是在匿名函数外面执行 |
---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* interface Composite { function add(child); function remove(child); ... } interface FormItem{ function save(); } var CompositeForm = function(id, method, action) { //implements Composite, FromItem ... }; CompositeForm.prototype.add = function(child) { ... } CompositeForm.prototype.remove= function(child) { ... } CompositeForm.prototype.save= function() { ... } |
---|
约定所有类明确声明实现了那些接口,和这些类打交道的对象可以针对这些声明做检查。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /* interface Composite { function add(child); function remove(child); ... } interface FormItem{ function save(); } var CompositeForm = function(id, method, action) { //implements Composite, FromItem this.implementsInterfaces = ['Composite', 'FromItem']; ... }; ... function addForm(formInstance) { if (!implements(formInstance, 'Composite', 'FormItem')) { throw new Error("object does not implement a required interface"); } } function implements(objects) { for (var i = 1; i < arguments.length; i++) { var interfaceName = arguments[i]; var interfaceFound = false; for (var j = 0; j < object.implementsInterfaces[j] == interfaceName) { interfaceFound = true; break; } if (!interfaceFound) { return false; } } return true; } |
---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | var Composite = new Intreface('Composite', ['add', 'remove']); var FormItem = new Interface('FormItem', ['save']); var CompositeForm = function(id, method, action) { ... }; function addForm(formInstance) { ensureImplements(formInstance, Composite, FormItem); ...... } var Interface = function(name, methods) { if (arguments.length != 2) { throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2."); } this.name = name; this.methods = []; for (var i = 0; len = methods.length; i < len; i++) { if (typeof methods[i] !== 'string') { throw new Error("Interface contructor expects method names to be " + "passed in as string"); } } this.methods.push(methods[i]); } Interface.ensureImplements = function(object) { if (arguments.length < 2) { throw Error("Functino Interface.ensureImplements called with " + arguments.length + "arguments, but expected at leaset 2."); } for (var i = 1, len = arguments.length; i < len; i++) { if (interface.constructor !== Interface) { throw new Error("Function Interface.ensureImplements expects arguments" + "two and above to be instances of Interface."); } } for (var j = 1, methodsLen = interface.methods.length; j < methodsLen; j++) { var method = interface.methods[j]; if(!object[method] || typeof object[method] !== 'function') { throw new Error("Function Interface.ensureImplements: object " + "does not implement the " + interface.name) + "interface.Method " + method + " was not found."); } } } |
---|