English  

كتب javascript world

اذا لم تجد ما تبحث عنه يمكنك استخدام كلمات أكثر دقة.

عرض المزيد

في JavaScript (معلومة)


الكائن الحرفي extend النهج | The Object-Literal and extend Approach

من الممكن تقنيًا إضافة سلوك إلى كائن من خلال ربطbinding الوظائف بالمفاتيح الموجودة في الكائنobject . ومع ذلك، فإن عدم الفصل بين الحالة والسلوك له عيوب:

  1. يخلط خصائص مجال النموذجmodel domain مع مجال التطبيقimplementation domain.
  2. عدم مشاركة السلوك المشترك. تقوم Metaobjects بحل هذه المشكلة عن طريق فصل خصائص المجال domain المحددة للكائنات عن خصائصها الخاصة بسلوكها.

يتم استخدام دالة التمديدextend function لخلط السلوك في:

'use strict'; const Halfling = function (fName, lName) { this.firstName = fName; this.lastName = lName; }; const mixin = { fullName() { return this.firstName + ' ' + this.lastName; }, rename(first, last) { this.firstName = first; this.lastName = last; return this; } }; // An extend function const extend = (obj, mixin) => { Object.keys(mixin).forEach(key => obj[key] = mixin[key]); return obj; }; const sam = new Halfling('Sam', 'Loawry'); const frodo = new Halfling('Freeda', 'Baggs'); // Mixin the other methods extend(Halfling.prototype, mixin); console.log(sam.fullName()); // Sam Loawry console.log(frodo.fullName()); // Freeda Baggs sam.rename('Samwise', 'Gamgee'); frodo.rename('Frodo', 'Baggins'); console.log(sam.fullName()); // Samwise Gamgee console.log(frodo.fullName()); // Frodo Baggins

امزج Mixin باستخدام ()Object.assign

'use strict'; // Creating an object const obj1 = { name: 'Marcus Aurelius', city: 'Rome', born: '121-04-26' }; // Mixin 1 const mix1 = { toString() { return `${this.name} was born in ${this.city} in ${this.born}`; }, age() { const year = new Date().getFullYear(); const born = new Date(this.born).getFullYear(); return year - born; } }; // Mixin 2 const mix2 = { toString() { return `${this.name} - ${this.city} - ${this.born}`; } }; // Adding the methods from mixins to the object using Object.assign() Object.assign(obj1, mix1, mix2); console.log(obj1.toString()); // Marcus Aurelius - Rome - 121-04-26 console.log(`His age is ${obj1.age()} as of today`); // His age is 1897 as of today

الوظيفة النقية والتفويض القائم على نهج خلط الطيران Flight-Mixin

على الرغم من أن النهج الموصوف أولاً منتشر في الغالب، فإن النهج التالي أقرب إلى ما يقدمه جوهر لغة جافا سكريبت بشكل أساسي - التفويض .

هناك نوعان من أنماط الوظائف الوظيفية يقومان بالفعل بالخدعة دون الحاجة إلى تنفيذ طرف ثالث extend .

'use strict'; // Implementation const EnumerableFirstLast = (function () { // function based module pattern. const first = function () { return this[0]؛ }, last = function () { return this[this.length - 1]؛ }; return function () { // function based Flight-Mixin mechanics ... this.first = first; // ... referring to ... this.last = last; // ... shared code. }; }()); // Application - explicit delegation: // applying [first] and [last] enumerable behavior onto [Array]'s [prototype]. EnumerableFirstLast.call(Array.prototype); // Now you can do: const a = [1, 2, 3]؛ a.first(); // 1 a.last(); // 3

المصدر: wikipedia.org