شرح صفوف ومحتوى الفقرة
في المشاريع الإنتاجية تتحقق أولاً من دعم المتصفح ثم تمرر مسار ملف مستقل ضمن نفس أصل الموقع مثل new Worker("/js/worker.js"). كما يتيح المتصفح حزمة أنيقة للاختبار المستقل عبر تغليف كود العامل داخل كائن Blob وتوليد رابط ذاكرة بـ URL.createObjectURL() لتشغيل خيط حقيقي دون ملف خارجي!
JavaScript
const code = 'onmessage = (e) => postMessage(e.data * 12);';
const blob = new Blob([code], { type: 'text/javascript' });
const worker = new Worker(URL.createObjectURL(blob));
const out = document.getElementById('wk-out');
worker.onmessage = (e) => {
out.textContent = 'Worker Computed: ' + e.data;
worker.terminate();
};
worker.postMessage(15);