From e6eb34287b1e7b1723acebe0814b9bc6e5139675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E6=9B=B0?= Date: Sat, 4 Jun 2016 23:36:48 +0800 Subject: [PATCH] Improve code style --- src/index.js | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/index.js b/src/index.js index 23babde..75e0e20 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,8 @@ import Vue from 'vue'; const Toast = Vue.extend(require('./toast.vue')); let toastPool = []; -let getAnInstance = function() { - if (toastPool.length > 0) { +const getAnInstance = () => { + if (toastPool.length) { let instance = toastPool[0]; toastPool.splice(0, 1); return instance; @@ -14,31 +14,27 @@ let getAnInstance = function() { }); }; -let returnAnInstance = function(instance) { +const returnAnInstance = (instance) => { if (instance) { toastPool.push(instance); } }; -export default function(options) { - options = options || {}; - - let message, position, duration, className, iconClass; - +export default (options = {}) => { if (typeof options === 'string') { - message = options; - duration = 3000; - position = 'middle'; - className = ''; - iconClass = ''; - } else { - message = options.message; - duration = options.duration || 3000; - position = options.position || 'middle'; - className = options.className || ''; - iconClass = options.iconClass || ''; + options = { + message: options + }; } + const { + className = '', + duration = 3000, + iconClass = '', + message, + position = 'middle' + } = options; + let instance = getAnInstance(); instance.message = message; @@ -46,9 +42,9 @@ export default function(options) { instance.className = className; instance.iconClass = iconClass; - Vue.nextTick(function() { + Vue.nextTick(() => { instance.$appendTo(document.body); - setTimeout(function() { + setTimeout(() => { instance.$remove(); returnAnInstance(instance); }, duration);