This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function aop(method, params, moduleName, functionName) { | |
| var that = this; | |
| // Такое формирование ключа кеширования приведено для простоты примера | |
| var key = moduleName + '_' + functionName + '_' + params[0]; | |
| cache.get(key, function(error, cachedResult) { | |
| // Получаем ссылку на callback-функцию (всегда передаётся последним параметром) | |
| var callback = params[params.length - 1]; | |
| if (error || !cachedResult) { | |
| // Результата в кеше не нашли, передаём управление в метод, подменяя callback-функцию | |
| params[params.length - 1] = function(error, result) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var NO_TOPIC = -1; | |
| var Topic; | |
| function Handler(s, t) { | |
| this.successor = s || null; | |
| this.topic = t || 0; | |
| } | |
| Handler.prototype = { | |
| handle: function () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var CarManager = { | |
| /* request information */ | |
| requestInfo:function (model, id) { | |
| return 'The purchase info for ' + model + ' with ID ' + id + ' is being processed...'; | |
| }, | |
| /* purchase the car */ | |
| buyVehicle:function (model, id) { | |
| return 'You have successfully purchased Item ' + id + ', a ' + model + '.'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //первый способ | |
| function StripedBall( ball ) { | |
| this._ball = ball | |
| } | |
| StripedBall.prototype = { | |
| constructor: StripedBall, | |
| draw: function() { | |
| this._ball.draw(); | |
| //и еще какие то действия |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var module = (function() { | |
| var _private = { | |
| i: 5, | |
| get: function() { | |
| console.log('Текущее значение:' + this.i); | |
| }, | |
| set: function(val) { | |
| this.i = val; | |
| }, | |
| run: function() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //способ первый | |
| function ShapeFactory(size, color) | |
| { | |
| this.size = size; | |
| this.color = color; | |
| } | |
| ShapeFactory.prototype = | |
| { | |
| constructor: ShapeFactory, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var agg = (function () { | |
| var index = 0, | |
| data = [1, 2, 3, 4, 5], | |
| length = data.length; | |
| return { | |
| next:function () { | |
| var element; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mediator = (function() { | |
| var subscribe = function(channel, fn) { | |
| if (!mediator.channels[channel]) mediator.channels[channel] = []; | |
| mediator.channels[channel].push({ context: this, callback: fn }); | |
| return this; | |
| }, | |
| publish = function(channel) { | |
| if (!mediator.channels[channel]) return false; | |
| var args = Array.prototype.slice.call(arguments, 1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function calculation(x, y) | |
| { | |
| var key = x.toString() + "|" + y.toString(); | |
| var result = 0; | |
| if (!calculation.memento[key]) | |
| { | |
| for (var i = 0; i < y; ++i) result += x; | |
| calculation.memento[key] = result; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Event = function() { | |
| this._observers = []; | |
| } | |
| Event.prototype = { | |
| raise: function (data) { | |
| for (var i in this._observers) { | |
| var item = this._observers[i]; | |
| item.observer.call(item.context, data); | |
| } | |
| }, |
NewerOlder