blob: 135948c80b758e60e8d831de8515b6f8903d6552 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
---
title: المنشئ (constructor)
slug: Web/JavaScript/Reference/Classes/constructor
tags:
- جافاسكريبت
translation_of: Web/JavaScript/Reference/Classes/constructor
---
<div>{{jsSidebar("Classes")}}</div>
<p dir="rtl">الـ <code>constructor</code> هي ميثود خاصة لإنشاء وتهيئة الاوبجكت(كائن) داخل (فئة)<code>class</code>.</p>
<div>{{EmbedInteractiveExample("pages/js/classes-constructor.html")}}</div>
<h2 dir="rtl" id="بناء_الجملة">بناء الجملة</h2>
<pre class="syntaxbox">constructor([arguments]) { ... }</pre>
<h2 dir="rtl" id="الوصف">الوصف</h2>
<p dir="rtl">يمكننا إستخدام تلك الميثود لمرة واحده فقط ، وفي حال إستخدمنا <code>constructor</code> ﻷكثر من مرة في نفس الفئة الـ class سيحدث {{jsxref("SyntaxError")}} إيرور .</p>
<p>A constructor can use the <code>super</code> keyword to call the constructor of a parent class.</p>
<p dir="rtl">إذا لم تحدد ميثود إنشاء سيتم تحديد منشئ بشكل تلقائي.</p>
<p><br>
<sup>If you do not specify a constructor method, a default constructor is used.</sup></p>
<h2 dir="rtl" id="أمثلة">أمثلة</h2>
<h3 dir="rtl" id="إٍستخدام_الميثود_(اسلوب)_الـconstructor">إٍستخدام الميثود (اسلوب) الـ<code>constructor </code></h3>
<p dir="rtl">ذلك الكود تم أخذه ولصقة من <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/classes-es6/index.html">classes sample</a> (<a href="https://googlechrome.github.io/samples/classes-es6/index.html">live demo</a>)</p>
<pre class="brush: js">class Square extends Polygon {
constructor(length) {
// Here, it calls the parent class' constructor with lengths
// provided for the Polygon's width and height
super(length, length);
// Note: In derived classes, super() must be called before you
// can use 'this'. Leaving this out will cause a reference error.
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
set area(value) {
this.area = value;
}
}</pre>
<h3 dir="rtl" id="مثال_آخر">مثال آخر</h3>
<p dir="rtl">إنظر إلى ذلك الكود</p>
<pre class="brush: js">class Polygon {
constructor() {
this.name = "Polygon";
}
}
class Square extends Polygon {
constructor() {
super();
}
}
class Rectangle {}
Object.setPrototypeOf(Square.prototype, Rectangle.prototype);
console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false
console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true
let newInstance = new Square();
console.log(newInstance.name); //Polygon</pre>
<p dir="rtl">هنا البروتوتيب فئة الـ <strong>Square</strong> تغيرت ولكن الكونستركتور (المنشئ) مبني على فئة الـ<strong>Polygon </strong>والتي تطلب عندما نقوم بإنشاء حالة مماثلة للـ <strong>Square </strong>مرة أخرى</p>
<h3 dir="rtl" id="الإنشاء_الإفتراضي">الإنشاء الإفتراضي</h3>
<p dir="rtl">ذكرنا سابقا أنه في حال عدم تحديدك لكونستركتور (منشئ) سيتم تحديد الكونستركتور بشكل إفتراضي ، وبالنسبة للكلاسز (الفئات) الاولية يكون الكونستركتور الخاص بها كما يلي :-</p>
<pre class="brush: js">constructor() {}
</pre>
<p dir="rtl">أما الفئات المشتقة فتكون بالشكل التالي</p>
<pre class="brush: js">constructor(...args) {
super(...args);
}</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES2015', '#sec-static-semantics-constructormethod', 'Constructor Method')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-static-semantics-constructormethod', 'Constructor Method')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 dir="rtl" id="دعم_المتصفحات">دعم المتصفحات</h2>
<p>{{Compat("javascript.classes.constructor")}}</p>
<h2 id="أنظر_أيضا">أنظر أيضا</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/super">super()</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/class"><code>class</code> expression</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/class"><code>class</code> declaration</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Classes">Classes</a></li>
</ul>
|