「javascriptをオブジェクトを使って書こう!」ということで今回の記事を書きます!
オブジェクトを使った書き方をすることにより,ソースコードの見やすさであったり,再利用性、保守なども行いやすくなります.
まず簡単なオブジェクトを説明します.
var hoge ={ a:'xxx', b:0, c:function(){ return 'yyy'; } };
上のコードは,a,b,cの3つのプロパティを持ったhogeというオブジェクトです.
それぞれのプロパティa,b,cには,「xxx」という文字列,「0」という整数,cのように関数を用いて書けば「yyy」という文字列を返すようになっています.
実行や操作も簡単です.
hoge.a = 'zzz';このようにやれば,'xxx'という文字列を'zzz'という文字列に書き換えられたり…
hoge.a;このようにやれば,オブジェクトを実行でき,「xxx」という文字列を出力してくれたり,
hoge.c();このようにやれば,関数化されたオブジェクトも実行でき「yyy」という文字列を出力できます.
またオブジェクトだと簡単に新しい機能の追加も可能です.
hoge.d = function(){ return 'xyz'; }cと同様に実行できます.
6月19日にktaroさんが関数の実行タイミングの記事を書いてくれていたのでぼくも,実行について少し書きます.
jQueryをインポートして,下のように書けばDocumentの読み込みが完了時に実行するされます.
(1回のみ)
(function() { var hoge ={ a:'xxx', b:0, c:function(){ return 'yyy'; } }; $(document).ready(function() { hoge.a; hoge.c(); }); })();
田胡・柴田研究室では,勉強会をよくやっています.
ソースコードの正しい書き方や,正しい変数のつけ方,また技術のことなど,日々教えてもらっています.
この記事に書いたオブジェクトを使ったの書き方も先輩に教えてもらったものです!
来てお話するだけでも,学ぶことのある研究室です.
研究室選びでのポイントとして,「自分の成長できる環境」で選ぶのがいいと思います.
就職活動でも,「こんなことやりました!」ってアピールできることは,この研究室に来ていればたくさんできますよ!
I’m excited to uncover this page. I need to thank you for your time for this, particularly fantastic read!! I definitely really liked every part of it and I also have you saved to fav to look at new information in your site.
返信削除AngularJS training in Pune
AngularJS Classes in Pune
Well explained………
返信削除Gets lot of information after reading your blog….
Thanks for sharing…..keep updating……..
Full Stack Developer Course with Placement
Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. Just have a look at this Air Hostess Training
返信削除This article offers a concise guide to using objects in JavaScript, highlighting benefits like improved code readability, reusability, and easier maintenance. It provides well-explained code examples, practical examples, and a personal touch, highlighting the flexibility and scalability of object-oriented programming. Bufetes de Abogados Accidentes de Camionaje
返信削除