感觉慢慢来了,看来一切都需要编码的累积。。。
Person = function() { var name = "default"; return { getName: function() { return name; }, setName: function(newName) { name = newName; } }; }; var john = Person(); console.log(john.getName()); john.setName("john"); console.log(john.getName()); var jack = Person(); console.log(jack.getName()); jack.setName("jack"); console.log(jack.getName()); var person = function() { var name = "default"; return { getName: function() { return name; }, setName: function(newName) { name = newName; } }; }(); console.log(person.name); console.log(person.getName()); console.log(person.setName("abruzzi")); console.log(person.getName()); function Base() { this.baseFunc = function() { console.log("base behavior"); }; } function Middle() { this.middleFunc = function() { console.log("middle behavior"); }; } Middle.prototype = new Base(); function Final() { this.finalFunc = function() { console.log("final behavior"); }; } Final.prototype = new Middle(); function test() { var obj = new Final(); obj.baseFunc(); obj.middleFunc(); obj.finalFunc(); } test(); var obj = {}; var ref = obj; obj.name = "objectA"; console.log(ref.name); obj = ["one", "two", "three"]; console.log(ref.name); console.log(obj.length); console.log(ref.length); var obj = {}; var ref1 = obj; var ref2 = obj; obj.func = "function"; console.log(ref1.func); console.log(ref2.func); function Shape(type) { this.type = type || "rect"; this.calc = function() { return "calc, " + this.type; }; } var triangle = new Shape("triangle"); var circle = new Shape("circle"); console.log(triangle.calc()); console.log(circle.calc()); function Person(name) { var address = "The Earth"; this.getAddress = function() { return address; }; this.name = name; } Person.prototype.getName = function() { return this.name; }; Person.prototype.setName = function(name) { this.name = name; }; Person.TAG = "Javascript TAG"; var jack = new Person("jack"); console.log(jack.name); console.log(jack.getName()); console.log(jack.address); console.log(jack.getAddress()); console.log(Person.TAG); var adder = function(num) { return function(y) { return num + y; }; }; var inc = adder(1); var dec = adder(-1); console.log(inc(99)); console.log(dec(99)); console.log(adder(100)(99)); console.log(adder(100)(-99)); var base = { name: "base", getInfo: function() { return this.id + ":" + this.name; }, }; var ext1 = { id: 0, name: "ext1", __proto__: base, }; var ext2 = { id: 9, name: "ext2", __proto__: base, }; console.log(ext1.id); console.log(ext1.getInfo()); console.log(ext2.id); console.log(ext2.getInfo()); function Task(id) { this.id = id; } Task.prototype.status = "STOPPED"; Task.prototype.execute = function(args) { return "execute task_" + this.id + "[" + this.status + "]:" + args; }; var task1 = new Task(1); var task2 = new Task(2); console.log(task1.execute("task1")); console.log(task2.execute("task2")); (function(name){ console.log("hello, " + name); })("jack"); function func(handle, message) { var id = 1; function doNothing(x) { return x; }; handle(message); }; func(console.log, "hello"); var topone = "top-level"; (function outter(){ var middle = "middle-level"; (function inner(){ var bottom = "bot-level"; console.log(topone + ">" + middle + ">" + bottom); })(); })(); var attribute = "attribute"; console.log(attribute); console.log(this.attribute); var global = this; var tom = { name: "Tome", home: "desine", getInfo: function(){ console.log(this.name + ", from " + this.home); }, }; tom.getInfo(); var jerry = { name: "jerry", getInfo: tom.getInfo, }; jerry.getInfo(); global.getInfo = tom.getInfo; global.getInfo();
时间: 2024-09-20 04:51:06