Skip to content

Commit b97c4de

Browse files
authored
Merge pull request #230 from msisaifu/Constructor-operator-new
Constructor, operator "new"
2 parents f0ae351 + 3892c50 commit b97c4de

File tree

1 file changed

+64
-64
lines changed
  • 1-js/04-object-basics/06-constructor-new

1 file changed

+64
-64
lines changed

Diff for: 1-js/04-object-basics/06-constructor-new/article.md

+64-64
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# Constructor, operator "new"
1+
# কন্সট্রাকটর এবং "new" অপারেটর
22

3-
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+
সাধারণত আমরা `{...}` এর সাহায্যে শুধুমাত্র একটি অবজেক্ট তৈরি করতে পারি। কিন্তু প্রায়সময় আমাদের একই ধরণের অনেক অবজেক্ট তৈরি করা লাগে, যেমন ইউজার বা টিচার অবজেক্ট।
44

5-
That can be done using constructor functions and the `"new"` operator.
5+
আমরা এটি করতে পারি কনস্ট্রাকটর ফাংশনের `"new"` অপারেটরের সাহায্যে।
66

7-
## Constructor function
7+
## কন্সট্রাকটর ফাংশন
88

9-
Constructor functions technically are regular functions. There are two conventions though:
9+
কন্সট্রাকটর ফাংশন দেখতে সাধারণ ফাংশনগুলোর মতই, তবে এটি দুটি নিয়ম মেনে চলে:
1010

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"` অপারেটর দিয়ে কল করতে হবে।
1313

14-
For instance:
14+
যেমন:
1515

1616
```js run
1717
function User(name) {
@@ -27,31 +27,31 @@ alert(user.name); // Jack
2727
alert(user.isAdmin); // false
2828
```
2929

30-
When a function is executed with `new`, it does the following steps:
30+
যখন কোন ফাংশন এ `new` অপারেটর ব্যবহার করা হয়, এটি নিম্নোক্ত বিষয়গুলো মেনে চলে:
3131

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` এর মান রিটার্ন করবে।
3535

36-
In other words, `new User(...)` does something like:
36+
নিচে `new User(...)` কীভাবে কাজ করছে তা দেখানো হয়েছে:
3737

3838
```js
3939
function User(name) {
4040
*!*
41-
// this = {}; (implicitly)
41+
// this = {}; (ইঞ্জিন এখানে this এ একটি খালি অবজেক্ট অ্যাসাইন করছে)
4242
*/!*
4343

44-
// add properties to this
44+
// প্রপার্টিযুক্ত হচ্ছে
4545
this.name = name;
4646
this.isAdmin = false;
4747

4848
*!*
49-
// return this; (implicitly)
49+
// return this; (সবার শেষে ইঞ্জিন this এর মান রিটার্ন করছে)
5050
*/!*
5151
}
5252
```
5353

54-
So `let user = new User("Jack")` gives the same result as:
54+
তাই `let user = new User("Jack")` এর মানটি হবে আমাদের নিচের `{...}` এর সাহায্যে ডিক্লেয়ার করা অবজেক্টের মত:
5555

5656
```js
5757
let user = {
@@ -60,88 +60,88 @@ let user = {
6060
};
6161
```
6262

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")` ইত্যাদি। সাধারণত এটি আরো বেশি পঠনযোগ্য এবং পরিবর্তনযোগ্য।
6464

65-
That's the main purpose of constructors -- to implement reusable object creation code.
65+
কন্সট্রাকটর ব্যবহারের প্রধান উদ্দেশ্যই হল পুনরায় ব্যবহারযোগ্য অবজেক্ট তৈরি সহজ করা।
6666

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` কী-ওয়ার্ড দ্বারা।
6868

6969
````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+
যদি আমাদের একটি কমপ্লেক্স অবজেক্ট শুধুমাত্র একবার তৈরি করা লাগে, তাহলে এটি অ্যানোনিমাস ফাংশন কন্ট্রাকটরের সাহায্যে তৈরি করতে পারি, এভাবে:
7171
7272
```js
7373
let user = new function() {
7474
this.name = "John";
7575
this.isAdmin = false;
7676
77-
// ...other code for user creation
78-
// maybe complex logic and statements
79-
// local variables etc
77+
// ...user এর অন্যান্য প্রপার্টি
78+
// লজিক এবং স্টেটমেন্ট
79+
// লোকাল ভ্যারিয়েবল ইত্যাদি
8080
};
8181
```
8282
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+
এখানে আমরা কন্সট্রাকটরটিকে পুনরায় কল করতে পারব না, কেননা এটি কোথাও সংরক্ষন করা হয়নি, তৈরি করেই কল করা হয়ে গিয়েছে। এই ধরণের এনক্যাপসুলেশন প্রয়োজন হয় একটি অবজেক্টের জন্য, যা পুনরায় ব্যবহার করা যাবে না।
8484
````
8585

86-
## Constructor mode test: new.target
86+
## Constructor কিনা যাচাই: new.target
8787

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+
এটি সাধারণত তেমন ব্যবহার করা হয়না, চাইলে এটি বাদ দিতে পারেন, তবে জেনে রাখা ভালো।
9090
```
9191

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`
9393

94-
It is empty for regular calls and equals the function if called with `new`:
94+
নিচের কোডে আমরা `User` কে `new` দ্বারা কল করলে `new.target` এর মান পাব একটি খালি অবজেক্ট অন্যথায় `undefined`:
9595

9696
```js run
9797
function User() {
9898
alert(new.target);
9999
}
100100

101-
// without "new":
101+
// "new" বাদে কল:
102102
*!*
103103
User(); // undefined
104104
*/!*
105105

106-
// with "new":
106+
// "new" অপারেটরের সাহায্যে কল:
107107
*!*
108108
new User(); // function User { ... }
109109
*/!*
110110
```
111111

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" কল করা হচ্ছে তা জানতে পারব।
113113

114-
We can also make both `new` and regular calls to do the same, like this:
114+
এবং আমরা চাইলে আমাদের "regular mode" এ কল করা ফাংশনকেও `new` দ্বারা আবদ্ধ করতে পারি, এভাবে:
115115

116116
```js run
117117
function User(name) {
118-
if (!new.target) { // if you run me without new
119-
return new User(name); // ...I will add new for you
118+
if (!new.target) { // new ব্যাতীত কল করলে এটি এক্সিকিউট হবে
119+
return new User(name); // ...new অ্যাসাইন হচ্ছে
120120
}
121121

122122
this.name = name;
123123
}
124124

125-
let john = User("John"); // redirects call to new User
125+
let john = User("John"); // new User কল হবে
126126
alert(john.name); // John
127127
```
128128

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` ছাড়া কল করলেও কাজ করবে।
130130

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` তৈরি হচ্ছে।
132132

133-
## Return from constructors
133+
## কন্সট্রাকটরের রিটার্ন
134134

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` কে রিটার্ন করে।
136136

137-
But if there is a `return` statement, then the rule is simple:
137+
কিন্তু যদি `return` স্টেটমেন্ট থাকে, তাহলে এটি নিম্নোক্ত নিয়ম মেনে চলে:
138138

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` করা হয় তাহলে এটি উপেক্ষা করবে।
141141

142-
In other words, `return` with an object returns that object, in all other cases `this` is returned.
142+
অন্যভাবে বলতে গেলে, যদি আমরা `this` এর পরিবর্তে কোন অবজেক্ট `return` করি তাহলে এটি ঐ অবজেক্টকেই রিটার্ন করে, অন্যথায় `this` রিটার্ন হয়।
143143

144-
For instance, here `return` overrides `this` by returning an object:
144+
যেমন, এখানে `return` `this` কে অন্য একটি অবজেক্ট দ্বারা ওভাররাইড করা হচ্ছে:
145145

146146
```js run
147147
function BigUser() {
@@ -154,7 +154,7 @@ function BigUser() {
154154
alert( new BigUser().name ); // Godzilla, got that object
155155
```
156156

157-
And here's an example with an empty `return` (or we could place a primitive after it, doesn't matter):
157+
আরো একটি উদাহরণ দেখা যাক যেখানে আমরা শুধু `return` স্টেটমেন্ট ব্যবহার করছি (অথবা এটি প্রিমিটিভ ভ্যালুও হতে পারে, যা উপেক্ষা করবে):
158158

159159
```js run
160160
function SmallUser() {
@@ -167,27 +167,27 @@ function SmallUser() {
167167
alert( new SmallUser().name ); // John
168168
```
169169

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` স্টেটমেন্ট ব্যবহার করা হয়না। তারপরও আমরা এটি আলোচনা করেছি যদি ব্যবহার করি তাহলে তা কেমন আচরণ করে তা জানার জন্য।
171171

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 অপারেটর ব্যবহারের সময় `()` ছাড়াও কন্সট্রাকটর ফাংশনকে কল করতে পারি, যদি এতে কোন আর্গুমেন্ট না থাকে:
174174
175175
```js
176176
let user = new User; // <-- no parentheses
177177
// same as
178178
let user = new User();
179179
```
180180
181-
Omitting parentheses here is not considered a "good style", but the syntax is permitted by specification.
181+
যদিও বন্ধনী ছাড়া কন্সট্রাকটর স্টেটমেন্ট লিখা উচিত না, তারপরও যে এই সিনট্যাক্স কাজ করে তা বুঝার জন্য এটি আলোচনা করা হল।
182182
````
183183

184-
## Methods in constructor
184+
## কনস্ট্রাকটরে মেথড কল
185185

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+
কনস্ট্রাকটর ফাংশনের মাধ্যমে আমরা সহজে রিইউজেবল অবজেক্ট তৈরি করতে পারি। এতে প্যারামিটার থাকতে পারে, যার মাধ্যমে নির্ধারণ করে দিতে পারি অবজেক্টটি কীভাবে তৈরি হবে।
187187

188-
Of course, we can add to `this` not only properties, but methods as well.
188+
অবশ্যই, আমরা `this` এ শুধুমাত্র প্রপার্টি না, মেথডও রাখতে পারি।
189189

190-
For instance, `new User(name)` below creates an object with the given `name` and the method `sayHi`:
190+
যেমন নিচের কোডে `new User(name)` এর একটি `name` প্রপার্টি আছে এবং একটি মেথড `sayHi`:
191191

192192
```js run
193193
function User(name) {
@@ -212,19 +212,19 @@ john = {
212212
*/
213213
```
214214

215-
To create complex objects, there's a more advanced syntax, [classes](info:classes), that we'll cover later.
215+
কমপ্লেক্স অবজেক্ট তৈরিতে আমরা [classes](info:classes) সিনট্যাক্স ব্যবহার করতে পারব, যার সম্পর্কে পরবর্তী অধ্যায়ে বিস্তারিত জানতে পারব।
216216

217-
## Summary
217+
## সারাংশ
218218

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` অবজেক্ট রিটার্ন হবে।
221221

222-
We can use constructor functions to make multiple similar objects.
222+
সাধারণত কনস্ট্রাকটর ফাংশন ব্যবহার করি একই টাইপের অনেক অবজেক্ট ডিক্লেয়ার করতে।
223223

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` ইত্যাদি। যার সম্পর্কে সামনের অধ্যায়গুলোতে বিস্তারিত জানতে পারব।
225225

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+
এই অধ্যায়ে আমরা বেসিক অবজেক্ট এবং কনস্ট্রাকটর সম্পর্কে জেনেছি। যা পরবর্তী অধ্যায়ে বিভিন্ন ডাটা টাইপ এবং ফাংশন সম্পর্কে বুঝতে জানা থাকা উচিত।
228228
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> এর অধ্যায়ে।
230230
```

0 commit comments

Comments
 (0)