আমি নতুন ত্রুটিযুক্ত বস্তু তৈরি করতে কনস্ট্রাক্টর প্যাটার্নটি ব্যবহার করেছি। আমি উদাহরণ হিসাবে প্রোটোটাইপ চেইন সংজ্ঞায়িত করেছি Error। MDN ত্রুটি নির্মাণকারী উল্লেখ দেখুন।
আপনি এই উপর এই স্নিপেট পরীক্ষা করতে পারবেন সারকথা ।
বাস্তবায়ন
// Creates user-defined exceptions
var CustomError = (function() {
'use strict';
//constructor
function CustomError() {
//enforces 'new' instance
if (!(this instanceof CustomError)) {
return new CustomError(arguments);
}
var error,
//handles the arguments object when is passed by enforcing a 'new' instance
args = Array.apply(null, typeof arguments[0] === 'object' ? arguments[0] : arguments),
message = args.shift() || 'An exception has occurred';
//builds the message with multiple arguments
if (~message.indexOf('}')) {
args.forEach(function(arg, i) {
message = message.replace(RegExp('\\{' + i + '}', 'g'), arg);
});
}
//gets the exception stack
error = new Error(message);
//access to CustomError.prototype.name
error.name = this.name;
//set the properties of the instance
//in order to resemble an Error instance
Object.defineProperties(this, {
stack: {
enumerable: false,
get: function() { return error.stack; }
},
message: {
enumerable: false,
value: message
}
});
}
// Creates the prototype and prevents the direct reference to Error.prototype;
// Not used new Error() here because an exception would be raised here,
// but we need to raise the exception when CustomError instance is created.
CustomError.prototype = Object.create(Error.prototype, {
//fixes the link to the constructor (ES5)
constructor: setDescriptor(CustomError),
name: setDescriptor('JSU Error')
});
function setDescriptor(value) {
return {
configurable: false,
enumerable: false,
writable: false,
value: value
};
}
//returns the constructor
return CustomError;
}());
, USAGE
কাস্টমআরার কনস্ট্রাক্টর বার্তাটি তৈরি করতে অনেকগুলি আর্গুমেন্ট গ্রহণ করতে পারে, যেমন
var err1 = new CustomError("The url of file is required"),
err2 = new CustomError("Invalid Date: {0}", +"date"),
err3 = new CustomError("The length must be greater than {0}", 4),
err4 = new CustomError("Properties .{0} and .{1} don't exist", "p1", "p2");
throw err4;
এবং কাস্টম ত্রুটিটি এইভাবে দেখায়:
