daily pastebin goal
76%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // eg
  2. //* 避免创建无效对象的解决方法是:在定义构造函数时加入判断
  3. var Book = function (title, time) {
  4.     //! 判断执行过程中this是否执行当前对象(如果是,说明是用new创建的)
  5.     if (this instanceof Book) {
  6.         this.title = title
  7.         this.time = time
  8.     } else {
  9.         //! 否则重新创建这个对象
  10.         return new Book(title, time)
  11.     }
  12. }
  13.  
  14. // Demo
  15. var instance = Book('title', 'name')
RAW Paste Data
Top