function createUser() { return { uid : "mock" + _.uniqueId(), first_name: "first_name_" + _.uniqueId(), last_name : "last_name_" + _.uniqueId() }; }
Backbone.View = function(options) { this.cid = _.uniqueId('view'); this._configure(options || {}); this._ensureElement(); this.delegateEvents(); this.initialize(options); };
ioChannel.on('connection', function(socket) { storage[channelId] = storage[channelId] || {}; storage[channelId][socket.id] = { 'name': _.uniqueId('User ') }; if (!_.isUndefined(channel.on) && !_.isUndefined(channel.on.channel)) _.each(channel.on.server || (channel.on.server = {}), function(callback, eventName) { socket.on(eventName, callback); S.channels[eventName] = {}; }); K.sockets[channelId] = socket; });
Backbone.Model = function(attributes, options) { var defaults; attributes || (attributes = {}); if (defaults = this.defaults) { if (_.isFunction(defaults)) defaults = defaults(); attributes = _.extend({}, defaults, attributes); } this.attributes = {}; this._escapedAttributes = {}; this.cid = _.uniqueId('c'); this.set(attributes, {silent : true}); this._changed = false; this._previousAttributes = _.clone(this.attributes); if (options && options.collection) this.collection = options.collection; this.initialize(attributes, options); };
/** * Created by tsq on 14-8-14. */ var _ = require("underscore")._; //noConflict: 把_变量的控制权预留给它原有的所有,返回一个引用给underscore对象 // random _.random(min, max) // 返回一个介于 min 和 max 之间(包含)的整数. 如果直传一个参数, 将会返回一个介于 0 和参数之间的整数 console.log(_.random(0, 100)); //mixin_.mixin(object) //允许您继承 Underscore 并加入您自己的功能函数. 传参 {名称: 函数} 来定义您的函数, 加入到 Underscore 对象中, 就像面向对象的封装. _.mixin({ capitalize: function(string, age){ return string + age } }); console.log(_('faf', 8).capitalize()); //unqueId 产生一个全局唯一id,以参数作为前缀 console.log(_.uniqueId('contact_')); console.log(_.uniqueId('contact_')); console.log(_.uniqueId('contact_')); console.log(_.uniqueId('contact_')); //escape,unescape:转义HTML字符串,反转到HTML字符串 console.log(_.escape('Curly, Larry & Moe')); //=> Curly, Larry & Moe console.log(_.unescape('Curly, Larry & Moe')); //=> Curly, Larry & Moe