এটি ইতিমধ্যে উত্তর হিসাবে, আমি কেবল জাভাস্ক্রিপ্টে একটি অবজেক্টের কনস্ট্রাক্টর পাওয়ার ক্ষেত্রে পদ্ধতির পার্থক্যগুলি উল্লেখ করতে চেয়েছিলাম। কনস্ট্রাক্টর এবং আসল অবজেক্ট / শ্রেণির নামের মধ্যে পার্থক্য রয়েছে। নিম্নলিখিতগুলি যদি আপনার সিদ্ধান্তের জটিলতায় জুড়ে থাকে তবে সম্ভবত আপনি খুঁজছেন instanceof। অথবা আপনার নিজেকে জিজ্ঞাসা করা উচিত "আমি কেন এটি করছি? আমি কি এটিই সমাধান করার চেষ্টা করছি?"
মন্তব্য:
obj.constructor.nameপুরোনো ব্রাউজারে পাওয়া যায় না। সমন্বয় (\w+)ES6 শৈলী শ্রেণীর সন্তুষ্ট করা উচিত নয়।
কোড:
var what = function(obj) {
return obj.toString().match(/ (\w+)/)[1];
};
var p;
// Normal obj with constructor.
function Entity() {}
p = new Entity();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name , "class:", what(p));
// Obj with prototype overriden.
function Player() { console.warn('Player constructor called.'); }
Player.prototype = new Entity();
p = new Player();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));
// Obj with constructor property overriden.
function OtherPlayer() { console.warn('OtherPlayer constructor called.'); }
OtherPlayer.constructor = new Player();
p = new OtherPlayer();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));
// Anonymous function obj.
p = new Function("");
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));
// No constructor here.
p = {};
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));
// ES6 class.
class NPC {
constructor() {
}
}
p = new NPC();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name , "class:", what(p));
// ES6 class extended
class Boss extends NPC {
constructor() {
super();
}
}
p = new Boss();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name , "class:", what(p));
ফলাফল:

কোড: https://jsbin.com/wikiji/edit?js,console