النظير في بيثون وجافا سكريبت. الجزء الرابع

الجزء الرابع من سلسلة مقالات عن نظائرها في Python و JavaScript.


في هذا الجزء: الحجج الوظيفية ، إنشاء والعمل مع الفئات ، والميراث ، وخطابات الضبط وخصائص الفئة.


ملخص الأجزاء السابقة:


  1. الجزء الأول : تحويل النوع ، عامل التشغيل الثلاثي ، الوصول إلى خاصية من خلال اسم الخاصية ، القواميس ، القوائم ، السلاسل ، سلسلة متسلسلة.
  2. الجزء الثاني : JSON ، النظامي ، أخطاء الاستثناء
  3. الجزء الثالث : Python و JS الحديث: أنماط السلسلة (خطوط f) ، تفريغ القوائم ، وظائف لامدا ، تكرار القائمة ، المولدات ، المجموعات.

وسيطات الدالة


تحتوي Python على مجموعة أدوات شاملة للعمل مع وسيطات الدوال - هناك قيم افتراضية ، وعدد متغير من الحجج الموضعية والمسمَّاة ( *args و **kwargs ).


عندما تقوم بتمرير قيمة إلى دالة ، يمكنك تحديد اسم الوسيطة التي سيتم تمرير هذه القيمة إليها. يحتوي JS أيضًا على هذه الميزة.


يمكن تحديد القيم الافتراضية لوسيطات الدوال في Python:


 from pprint import pprint def report(post_id, reason='not-relevant'): pprint({'post_id': post_id, 'reason': reason}) report(42) report(post_id=24, reason='spam') 

وبالمثل في شبيبة:


 function report(post_id, reason='not-relevant') { console.log({post_id: post_id, reason: reason}); } report(42); report(post_id=24, reason='spam'); 

يمكن معالجة الوسائط الموضعية في Python باستخدام عامل التشغيل * :


 from pprint import pprint def add_tags(post_id, *tags): pprint({'post_id': post_id, 'tags': tags}) add_tags(42, 'python', 'javascript', 'django') 

في JS ، تتم معالجة الوسائط الموضعية باستخدام عامل التشغيل ... :


 function add_tags(post_id, ...tags) { console.log({post_id: post_id, tags: tags}); } add_tags(42, 'python', 'javascript', 'django'); 

غالبًا ما يتم استخدام الوسائط المسماة في Python عندما تحتاج إلى تمرير عدد متغير من الوسائط:


 from pprint import pprint def create_post(**options): pprint(options) create_post( title='Hello, World!', content='This is our first post.', is_published=True, ) create_post( title='Hello again!', content='This is our second post.', ) 

يتم تنفيذ تمرير مجموعة من الوسيطات المسماة إلى JS باستخدام قاموس ( options في هذا المثال):


 function create_post(options) { console.log(options); } create_post({ 'title': 'Hello, World!', 'content': 'This is our first post.', 'is_published': true }); create_post({ 'title': 'Hello again!', 'content': 'This is our second post.' }); 

الطبقات والوراثة


Python لغة موجهة للكائنات. بدءًا من ECMAScript 6 ، تسمح لك JS أيضًا بكتابة تعليمات برمجية موجهة للكائنات دون أي حيل وبناء.


بيثون نقوم بإنشاء فئة ومنشئ وطريقة للتمثيل النصي للكائن:


 class Post(object): def __init__(self, id, title): self.id = id self.title = title def __str__(self): return self.title post = Post(42, 'Hello, World!') isinstance(post, Post) == True print(post) # Hello, World! 

إجراءات مماثلة على JS:


 class Post { constructor (id, title) { this.id = id; this.title = title; } toString() { return this.title; } } post = new Post(42, 'Hello, World!'); post instanceof Post === true; console.log(post.toString()); // Hello, World! 

قم بإنشاء فئتين Article ووصلة في Python والتي سوف ترث من فئة Post . قد تلاحظ أننا نستخدم الوظيفة super للوصول إلى طرق الفئة الأساسية للنشر:


 class Article(Post): def __init__(self, id, title, content): super(Article, self).__init__(id, title) self.content = content class Link(Post): def __init__(self, id, title, url): super(Link, self).__init__(id, title) self.url = url def __str__(self): return '{} ({})'.format( super(Link, self).__str__(), self.url, ) article = Article(1, 'Hello, World!', 'This is my first article.') link = Link(2, 'DjangoTricks', 'https://djangotricks.blogspot.com') isinstance(article, Post) == True isinstance(link, Post) == True print(link) # DjangoTricks (https://djangotricks.blogspot.com) 

نفس الشيء في شبيبة:


 class Article extends Post { constructor (id, title, content) { super(id, title); this.content = content; } } class Link extends Post { constructor (id, title, url) { super(id, title); this.url = url; } toString() { return super.toString() + ' (' + this.url + ')'; } } article = new Article(1, 'Hello, World!', 'This is my first article.'); link = new Link(2, 'DjangoTricks', 'https://djangotricks.blogspot.com'); article instanceof Post === true; link instanceof Post === true; console.log(link.toString()); // DjangoTricks (https://djangotricks.blogspot.com) 

خصائص الفئة: الحروف والمستوطنون.


في البرمجة الموجهة للكائنات ، تحتوي الفئات على سمات وأساليب وخصائص. الخصائص هي مزيج من السمات والأساليب. يمكنك التعامل مع الخصائص كسمات ، ولكن في مكان ما داخلها ، يسمونها طرقًا خاصة تسمى getters / setters لمعالجة بيانات محددة.
في هذا المثال ، تُظهر Python الطريقة الأساسية لوصف المُدرب والمُحضر لخاصية slug باستخدام الديكورات:


 class Post(object): def __init__(self, id, title): self.id = id self.title = title self._slug = '' @property def slug(self): return self._slug @slug.setter def slug(self, value): self._slug = value post = Post(1, 'Hello, World!') post.slug = 'hello-world' print(post.slug) 

في JS ، يمكن وصف كل من getter و setter لخاصية slug على النحو التالي:


 class Post { constructor (id, title) { this.id = id; this.title = title; this._slug = ''; } set slug(value) { this._slug = value; } get slug() { return this._slug; } } post = new Post(1, 'Hello, World!'); post.slug = 'hello-world'; console.log(post.slug); 

الاستنتاجات


  • في كلتا اللغتين ، يمكنك وصف القيم الافتراضية لوسيطات الدوال.
  • في كلتا اللغتين ، يمكنك تمرير عدد عشوائي من الحجج المسماة أو الموضعية إلى دالة.
  • تدعم كلتا اللغتين نموذج البرمجة الشيئية.

حسنًا ، في النهاية ، يقترح مؤلف المنشور الأصلي شراء منه ملف pdf يحتوي على أوراق غش ملونة للبايثون وجافا سكريبت مقابل خمسة عشر دولارًا .


اطبع وتعلق على الحائط أو اصنع طائرة ورقية - أنت تقرر!

Source: https://habr.com/ru/post/ar419271/


All Articles