You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The regular`{...}`syntax allows to create one object. But often we need to create many similar objects, like multiple users or menu items and so on.
3
+
সাধারণত আমরা`{...}`এর সাহায্যে শুধুমাত্র একটি অবজেক্ট তৈরি করতে পারি। কিন্তু প্রায়সময় আমাদের একই ধরণের অনেক অবজেক্ট তৈরি করা লাগে, যেমন ইউজার বা টিচার অবজেক্ট।
4
4
5
-
That can be done using constructor functions and the `"new"`operator.
5
+
আমরা এটি করতে পারি কনস্ট্রাকটর ফাংশনের `"new"`অপারেটরের সাহায্যে।
6
6
7
-
## Constructor function
7
+
## কন্সট্রাকটর ফাংশন
8
8
9
-
Constructor functions technically are regular functions. There are two conventions though:
9
+
কন্সট্রাকটর ফাংশন দেখতে সাধারণ ফাংশনগুলোর মতই, তবে এটি দুটি নিয়ম মেনে চলে:
10
10
11
-
1.They are named with capital letter first.
12
-
2.They should be executed only with `"new"`operator.
11
+
1.ফাংশনের নামটি বড় হাতের অক্ষর দিয়ে শুরু হয়। যেমন `user` এর পরিবর্তে `User`
12
+
2.এদের ডিক্লেয়ার করার সময় অর্থাৎ ফাংশন কল করার সময় `"new"`অপারেটর দিয়ে কল করতে হবে।
13
13
14
-
For instance:
14
+
যেমন:
15
15
16
16
```js run
17
17
functionUser(name) {
@@ -27,31 +27,31 @@ alert(user.name); // Jack
27
27
alert(user.isAdmin); // false
28
28
```
29
29
30
-
When a function is executed with `new`, it does the following steps:
30
+
যখন কোন ফাংশন এ `new` অপারেটর ব্যবহার করা হয়, এটি নিম্নোক্ত বিষয়গুলো মেনে চলে:
31
31
32
-
1.A new empty object is created and assigned to `this`.
33
-
2.The function body executes. Usually it modifies`this`, adds new properties to it.
34
-
3.The value of`this`is returned.
32
+
1.একটি নতুন খালি অবজেক্ট তৈরি `this` এ অ্যাসাইন হবে।
33
+
2.এরপর ফাংশনের বডি এক্সিকিউট হবে। সাধারণত এটি`this` এ রূপান্তর হবে, এবং নতুন প্রপার্টি সংযুক্ত হবে।
34
+
3.এবং সবার শেষে`this`এর মান রিটার্ন করবে।
35
35
36
-
In other words, `new User(...)`does something like:
36
+
নিচে `new User(...)`কীভাবে কাজ করছে তা দেখানো হয়েছে:
37
37
38
38
```js
39
39
functionUser(name) {
40
40
*!*
41
-
// this = {}; (implicitly)
41
+
// this = {}; (ইঞ্জিন এখানে this এ একটি খালি অবজেক্ট অ্যাসাইন করছে)
42
42
*/!*
43
43
44
-
//add properties to this
44
+
//প্রপার্টিযুক্ত হচ্ছে
45
45
this.name= name;
46
46
this.isAdmin=false;
47
47
48
48
*!*
49
-
// return this; (implicitly)
49
+
// return this; (সবার শেষে ইঞ্জিন this এর মান রিটার্ন করছে)
50
50
*/!*
51
51
}
52
52
```
53
53
54
-
So`let user = new User("Jack")`gives the same result as:
54
+
তাই`let user = new User("Jack")`এর মানটি হবে আমাদের নিচের `{...}` এর সাহায্যে ডিক্লেয়ার করা অবজেক্টের মত:
55
55
56
56
```js
57
57
let user = {
@@ -60,88 +60,88 @@ let user = {
60
60
};
61
61
```
62
62
63
-
Now if we want to create other users, we can call `new User("Ann")`, `new User("Alice")`and so on. Much shorter than using literals every time, and also easy to read.
63
+
এখন আমরা যদি অন্য ইউজার তৈরি করতে চাই, তাহলে এভাবে কল করতে পারে `new User("Ann")`, `new User("Alice")`ইত্যাদি। সাধারণত এটি আরো বেশি পঠনযোগ্য এবং পরিবর্তনযোগ্য।
64
64
65
-
That's the main purpose of constructors -- to implement reusable object creation code.
65
+
কন্সট্রাকটর ব্যবহারের প্রধান উদ্দেশ্যই হল পুনরায় ব্যবহারযোগ্য অবজেক্ট তৈরি সহজ করা।
66
66
67
-
Let's note once again -- technically, any function can be used as a constructor. That is: any function can be run with `new`, and it will execute the algorithm above. The "capital letter first" is a common agreement, to make it clear that a function is to be run with `new`.
67
+
একটি ব্যাপার সম্পর্কে পরিষ্কার ধারণা থাকা দরকার। সাধারণত, যে কোন ফাংশনকে আমরা কন্সট্রাকটর ফাংশন হিসেবে ব্যবহার করতে পারি। অর্থাৎ যেকোন ফাংশনকে `new` দ্বারা কল করা হলে এটি কন্সট্রাকটর ফাংশন হিসেবে কাজ করবে। অর্থাৎ আপনি যদি ফাংশনের নামের সব অক্ষর ছোট হাতের ব্যবহার করেন তাও কাজ করবে, তবে কন্সট্রাকটর ফাংশনকে বড় হাতের অক্ষর দিয়ে শুরু করা সার্বজনীন স্বীকৃত, এবং এটি নির্দেশ করে আমাদের ফাংশনটি ডিক্লেয়ার করতে হবে `new` কী-ওয়ার্ড দ্বারা।
68
68
69
69
````smart header="new function() { ... }"
70
-
If we have many lines of code all about creation of a single complex object, we can wrap them in constructor function, like this:
70
+
যদি আমাদের একটি কমপ্লেক্স অবজেক্ট শুধুমাত্র একবার তৈরি করা লাগে, তাহলে এটি অ্যানোনিমাস ফাংশন কন্ট্রাকটরের সাহায্যে তৈরি করতে পারি, এভাবে:
71
71
72
72
```js
73
73
let user = new function() {
74
74
this.name = "John";
75
75
this.isAdmin = false;
76
76
77
-
// ...other code for user creation
78
-
// maybe complex logic and statements
79
-
// local variables etc
77
+
// ...user এর অন্যান্য প্রপার্টি
78
+
// লজিক এবং স্টেটমেন্ট
79
+
// লোকাল ভ্যারিয়েবল ইত্যাদি
80
80
};
81
81
```
82
82
83
-
The constructor can't be called again, because it is not saved anywhere, just created and called. So this trick aims to encapsulate the code that constructs the single object, without future reuse.
83
+
এখানে আমরা কন্সট্রাকটরটিকে পুনরায় কল করতে পারব না, কেননা এটি কোথাও সংরক্ষন করা হয়নি, তৈরি করেই কল করা হয়ে গিয়েছে। এই ধরণের এনক্যাপসুলেশন প্রয়োজন হয় একটি অবজেক্টের জন্য, যা পুনরায় ব্যবহার করা যাবে না।
84
84
````
85
85
86
-
## Constructor mode test: new.target
86
+
## Constructor কিনা যাচাই: new.target
87
87
88
-
```smart header="Advanced stuff"
89
-
The syntax from this section is rarely used, skip it unless you want to know everything.
88
+
```smart header="অ্যাডভান্স টপিক"
89
+
এটি সাধারণত তেমন ব্যবহার করা হয়না, চাইলে এটি বাদ দিতে পারেন, তবে জেনে রাখা ভালো।
90
90
```
91
91
92
-
Inside a function, we can check whether it was called with `new`or without it, using a special `new.target` property.
92
+
একটি ফাংশনের মধ্যে আমরা চাইলে যাচাই করতে পারি, এটি `new`দ্বারা কল করা হয়েছে নাকি হয়নি, এজন্য একটি বিশেষ প্রপার্টি আছে `new.target`।
93
93
94
-
It is empty for regular calls and equals the function if called with `new`:
94
+
নিচের কোডে আমরা `User` কে `new` দ্বারা কল করলে `new.target` এর মান পাব একটি খালি অবজেক্ট অন্যথায় `undefined`:
95
95
96
96
```js run
97
97
functionUser() {
98
98
alert(new.target);
99
99
}
100
100
101
-
//without "new":
101
+
// "new" বাদে কল:
102
102
*!*
103
103
User(); // undefined
104
104
*/!*
105
105
106
-
//with "new":
106
+
// "new" অপারেটরের সাহায্যে কল:
107
107
*!*
108
108
newUser(); // function User { ... }
109
109
*/!*
110
110
```
111
111
112
-
That can be used inside the function to know whether it was called with `new`, "in constructor mode", or without it, "in regular mode".
112
+
উপরের টেকনিকটি খাটিয়ে আমরা কোন ফাংশনকে "constructor mode" এ নাকি "regular mode" কল করা হচ্ছে তা জানতে পারব।
113
113
114
-
We can also make both `new` and regular calls to do the same, like this:
114
+
এবং আমরা চাইলে আমাদের "regular mode" এ কল করা ফাংশনকেও `new` দ্বারা আবদ্ধ করতে পারি, এভাবে:
115
115
116
116
```js run
117
117
functionUser(name) {
118
-
if (!new.target) { //if you run me without new
119
-
returnnewUser(name); // ...I will add new for you
118
+
if (!new.target) { //new ব্যাতীত কল করলে এটি এক্সিকিউট হবে
119
+
returnnewUser(name); // ...new অ্যাসাইন হচ্ছে
120
120
}
121
121
122
122
this.name= name;
123
123
}
124
124
125
-
let john =User("John"); //redirects call to new User
125
+
let john =User("John"); // new User কল হবে
126
126
alert(john.name); // John
127
127
```
128
128
129
-
This approach is sometimes used in libraries to make the syntax more flexible. So that people may call the function with or without `new`, and it still works.
129
+
অনেকসময় এটি ব্যবহার করা হয় লাইব্রেরীগুলোর সিনট্যাক্স আরো সহজবোধ্য করতে। ফলে আমরা কোন ফাংশনকে `new` ছাড়া কল করলেও কাজ করবে।
130
130
131
-
Probably not a good thing to use everywhere though, because omitting `new`makes it a bit less obvious what's going on. With `new`we all know that the new object is being created.
131
+
তবে এটি কোন ভালো আইডিয়া না, কেননা `new`বাদে কল করলে আমাদের কোড কীভাবে কাজ করছে তা জানা কিছুটা দুর্বোধ্য হয়ে যাবে। কেননা `new`দ্বারা কল করলে আমরা বুঝতে পারি একটি `Object` তৈরি হচ্ছে।
132
132
133
-
## Return from constructors
133
+
## কন্সট্রাকটরের রিটার্ন
134
134
135
-
Usually, constructors do not have a `return`statement. Their task is to write all necessary stuff into `this`, and it automatically becomes the result.
135
+
সাধারণত কন্সট্রাকটরের `return`স্টেটমেন্ট থাকে না। এর সব কাজ `this` এ সম্পন্ন হয়ে সবার শেষে `this` কে রিটার্ন করে।
136
136
137
-
But if there is a `return`statement, then the rule is simple:
137
+
কিন্তু যদি `return`স্টেটমেন্ট থাকে, তাহলে এটি নিম্নোক্ত নিয়ম মেনে চলে:
138
138
139
-
-If `return`is called with an object, then the object is returned instead of `this`.
140
-
-If `return`is called with a primitive, it's ignored.
139
+
-যদি কোন অবজেক্টকে `return`করা হয় তাহলে এটি `this` এর পরিবর্তে ঐ অবজেক্টকে রিটার্ন করবে।
140
+
-যদি কোন প্রিমিটিভ ভ্যালু `return`করা হয় তাহলে এটি উপেক্ষা করবে।
141
141
142
-
In other words, `return` with an object returns that object, in all other cases `this`is returned.
142
+
অন্যভাবে বলতে গেলে, যদি আমরা `this` এর পরিবর্তে কোন অবজেক্ট `return` করি তাহলে এটি ঐ অবজেক্টকেই রিটার্ন করে, অন্যথায় `this`রিটার্ন হয়।
143
143
144
-
For instance, here`return`overrides`this`by returning an object:
144
+
যেমন, এখানে`return`এ`this`কে অন্য একটি অবজেক্ট দ্বারা ওভাররাইড করা হচ্ছে:
145
145
146
146
```js run
147
147
functionBigUser() {
@@ -154,7 +154,7 @@ function BigUser() {
154
154
alert( newBigUser().name ); // Godzilla, got that object
155
155
```
156
156
157
-
And here's an example with an empty `return`(or we could place a primitive after it, doesn't matter):
157
+
আরো একটি উদাহরণ দেখা যাক যেখানে আমরা শুধু `return`স্টেটমেন্ট ব্যবহার করছি (অথবা এটি প্রিমিটিভ ভ্যালুও হতে পারে, যা উপেক্ষা করবে):
158
158
159
159
```js run
160
160
functionSmallUser() {
@@ -167,27 +167,27 @@ function SmallUser() {
167
167
alert( newSmallUser().name ); // John
168
168
```
169
169
170
-
Usually constructors don't have a `return`statement. Here we mention the special behavior with returning objects mainly for the sake of completeness.
170
+
সাধারণত কন্সট্রাকটরে `return`স্টেটমেন্ট ব্যবহার করা হয়না। তারপরও আমরা এটি আলোচনা করেছি যদি ব্যবহার করি তাহলে তা কেমন আচরণ করে তা জানার জন্য।
171
171
172
-
````smart header="Omitting parentheses"
173
-
By the way, we can omit parentheses after `new`, if it has no arguments:
172
+
````smart header="প্রথমবন্ধনী ছাড়া কল"
173
+
আমরা new অপারেটর ব্যবহারের সময় `()` ছাড়াও কন্সট্রাকটর ফাংশনকে কল করতে পারি, যদি এতে কোন আর্গুমেন্ট না থাকে:
174
174
175
175
```js
176
176
let user = new User; // <-- no parentheses
177
177
// same as
178
178
let user = new User();
179
179
```
180
180
181
-
Omitting parentheses here is not considered a "good style", but the syntax is permitted by specification.
181
+
যদিও বন্ধনী ছাড়া কন্সট্রাকটর স্টেটমেন্ট লিখা উচিত না, তারপরও যে এই সিনট্যাক্স কাজ করে তা বুঝার জন্য এটি আলোচনা করা হল।
182
182
````
183
183
184
-
## Methods in constructor
184
+
## কনস্ট্রাকটরে মেথড কল
185
185
186
-
Using constructor functions to create objects gives a great deal of flexibility. The constructor function may have parameters that define how to construct the object, and what to put in it.
186
+
কনস্ট্রাকটর ফাংশনের মাধ্যমে আমরা সহজে রিইউজেবল অবজেক্ট তৈরি করতে পারি। এতে প্যারামিটার থাকতে পারে, যার মাধ্যমে নির্ধারণ করে দিতে পারি অবজেক্টটি কীভাবে তৈরি হবে।
187
187
188
-
Of course, we can add to `this`not only properties, but methods as well.
188
+
অবশ্যই, আমরা `this`এ শুধুমাত্র প্রপার্টি না, মেথডও রাখতে পারি।
189
189
190
-
For instance, `new User(name)`below creates an object with the given `name`and the method`sayHi`:
190
+
যেমন নিচের কোডে `new User(name)`এর একটি `name`প্রপার্টি আছে এবং একটি মেথড`sayHi`:
191
191
192
192
```js run
193
193
functionUser(name) {
@@ -212,19 +212,19 @@ john = {
212
212
*/
213
213
```
214
214
215
-
To create complex objects, there's a more advanced syntax, [classes](info:classes), that we'll cover later.
215
+
কমপ্লেক্স অবজেক্ট তৈরিতে আমরা [classes](info:classes) সিনট্যাক্স ব্যবহার করতে পারব, যার সম্পর্কে পরবর্তী অধ্যায়ে বিস্তারিত জানতে পারব।
216
216
217
-
## Summary
217
+
## সারাংশ
218
218
219
-
-Constructor functions or, briefly, constructors, are regular functions, but there's a common agreement to name them with capital letter first.
220
-
-Constructor functions should only be called using `new`. Such a call implies a creation of empty `this`at the start and returning the populated one at the end.
219
+
-কন্সট্রাকটর ফাংশন এবং রেগুলার ফাংশনের মাঝে পার্থক্য হল কন্সট্রাকটর ফাংশন ক্যামেল কেসে লিখা হয়।
220
+
-কন্সট্রাকটর ফাংশনকে `new` দ্বারা কল করা হয়। এইক্ষেত্রে ফাংশনের শুরুতে একটি খালি `this`অবজেক্ট তৈরি হবে এবং সবার শেষে `this` অবজেক্ট রিটার্ন হবে।
221
221
222
-
We can use constructor functions to make multiple similar objects.
222
+
সাধারণত কনস্ট্রাকটর ফাংশন ব্যবহার করি একই টাইপের অনেক অবজেক্ট ডিক্লেয়ার করতে।
223
223
224
-
JavaScript provides constructor functions for many built-in language objects: like `Date` for dates, `Set` for sets and others that we plan to study.
224
+
জাভাস্ক্রিপ্টে অনেক বিল্ট-ইন কনস্ট্রাকটর ফাংশন আছে, যেমন `Date`, `Set`, `Map` ইত্যাদি। যার সম্পর্কে সামনের অধ্যায়গুলোতে বিস্তারিত জানতে পারব।
225
225
226
-
```smart header="Objects, we'll be back!"
227
-
In this chapter we only cover the basics about objects and constructors. They are essential for learning more about data types and functions in the next chapters.
226
+
```smart header="পরবর্তী অধ্যায়গুলোতে অবজেক্ট নিয়ে আরো বিস্তারিত জানব!"
227
+
এই অধ্যায়ে আমরা বেসিক অবজেক্ট এবং কনস্ট্রাকটর সম্পর্কে জেনেছি। যা পরবর্তী অধ্যায়ে বিভিন্ন ডাটা টাইপ এবং ফাংশন সম্পর্কে বুঝতে জানা থাকা উচিত।
228
228
229
-
After we learn that, we return to objects and cover them in-depth in the chapters <info:prototypes> and <info:classes>.
229
+
এর শেষে আমরা অবজেক্ট নিয়ে আরো বিষদ আলোচনা করেছি <info:prototypes> এবং <info:classes> এর অধ্যায়ে।
0 commit comments