2017-07-14 54 views

回答

1

您可以使用var obj = new this(attrs);如下證明:

function someDBcalls(id) { 
 
    return { id, success: true }; 
 
} 
 

 
class Model { 
 
    static find(id) { 
 
    var attrs = someDBcalls(id); 
 
    var obj = new this(attrs); 
 
    return obj; 
 
    } 
 
} 
 

 
class User extends Model { 
 
    constructor(attrs) { 
 
    super(); 
 
    // initialize class. 
 
    console.log(attrs); 
 
    } 
 
} 
 

 
var user = User.find(1); // shall return an User object. 
 
console.log(user instanceof User);

+0

感謝,它的工作原理 –

相關問題