メモ †
あんちょこ †HTML に入れるときの基本 †<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <script type="text/javascript"><!-- // ここにスクリプト // --></script> </head> <body> <noscript> <p>JavaScript を ON にしてね。</p> </noscript> </body> </html>
変数の定義、参照 †var x; x = 1; var x = 1; alert(x); var を書かず、いきなり x = 1 と書いた場合、スコープが持つ暗黙のオブジェクトに対するプロパティ追加とみなされる。(一般には Global オブジェクトに追加/更新 変数のスコープ †var x = 1;
function do_test()
{
alert(x); // undefined
var x = 2;
alert(x); // 2
if (true) {
var x = 3;
alert(x); // 3
}
alert(x); // 3 (注意!)
}
関数の定義、参照。 †function myFunc(arg)
{
alert(arg);
}
myFunc("Hello, world!");
関数オブジェクトを変数に取り込む †function myFunc(arg)
{
alert(arg);
}
var alertObj = myFunc
alertObj("Hello, world");
関数の引数の数が不定の場合の取り扱い †
関数を動的に定義する。 †var myFunc = new Function("arg", "alert(arg);");
myFunc("Hello, world!");
以下と同等。 function myFunc(arg)
{
alert(arg);
}
myFunc("Hello, world!");
関数スコープに動的に変数を追加する。 †function myFunc()
{
eval("var x");
x = 1;
}
クラス定義方法色々 †
クラス定義せずに、いきなり複数のプロパティ持ったインスタンスを生成する。 †var point = { x:1, y:2 };
空のコンストラクタ †コンストラクタはただの関数。 function MyClass()
{
}
インスタンスを生成する。 †function MyClass()
{
}
var myObj = new MyClass();
new は、まず、空のインスタンスを生成して、関数 MyClassの this にそれを引き渡す。 もしも new を使わないと this には Global インスタンスが使われる。(要注意!) コンストラクタでメンバ変数を定義する。 †this に相当するインスタンスは new 演算子が作って渡してくれる。後はそこにプロパティを追加する。 function MyClass()
{
this.message = "Hello";
}
コンストラクタで引数を受け取る。 †function Point(x, y)
{
this.x = x;
this.y = y;
}
var myPoint = new Point(1, 2);
メンバを定義する。(プロトタイプ版) †function MyClass()
{
this.initialize.apply(this, arguments) // ← 委譲
}
MyClass.prototype = {
initialize: function(n, m) {
this.n = n;
this.m = m;
}, // ←ハッシュなので、カンマ区切りである点に注意。
myMethod: function(x) {
...
},
foo: function(a, b) {
...
}
}
メンバを追加する。(プロトタイプ版) †function MyClass()
{
}
// プロトタイプとして変数を定義すると、
// インスタンスに変数が未設定の場合には、
// この値が参照されることになる。デフォルト値のイメージ。
MyClass.prototype.myVar = null;
MyClass.prototype.myMethod = function(x) {
...
}; // ← 代入なのでここはセミコロン
MyClass.prototype.foo = function(a, b) {
...
};
メンバを定義する。(非プロトタイプ版) †
function MyClass()
{
this.myVar = null;
this.myMethod = function(x) {
...
};
this.foo = function(a, b) {
...
};
}
メンバ関数を追加する。(非プロトタイプ版) †function MyClass()
{
}
MyClass.myMethod = function(x) {
...
}
MyClass.foo = function(a, b) {
...
}
変数はプロトタイプで定義するべきか、コンストラクタで this に定義するべきか? †// this に定義
function MyClass()
{
this.myInstanceVar = 0;
}
// prototype に定義
this.prototype.myPrototypeVar = 0;
インスタンスのメンバの参照 †myObj.myProp あるいは myObj["myProp"] 文字列が使えるので、文字列での動的な処理変更ができる。 コンストラクタにメンバを動的に追加する。 †MyClass.prototype.message = "Hello"; コンストラクタからメンバを動的に削除する。 †delete MyClass.prototype.message; インスタンスにメンバを動的に追加する。 †function Point(x, y)
{
this.x = x;
this.y = y;
}
var myPoint = new Point(1, 2);
myPoint.z = 3; // いいのか悪いのか…
インスタンスからメンバを動的に削除する。 †function Point(x, y)
{
this.x = x;
this.y = y;
}
var myPoint = new Point(1, 2);
delete myPoint.y; // いいのか悪いのか…
クラスのインスタンスかどうかを判定する。 †if (myObj instanceof MyClass) インスタンスにメンバがあるかどうかを判定する。 †if ("myMember" in myObj)
インスタンス自身にメンバがあるかどうかを判定する。 †function MyClass()
{
}
MyClass.prototype.myMember = 1;
var myObj = new MyClass();
alert(myObj.hasOwnProperty("myMember")); // false
alert(myObj.myMember);
// myObj には myMember メンバはあるが、実際には prototype が所持している。
myObj.myMember = 2;
alert(myObj.hasOwnProperty("myMember")); // true
alert(myObj.myMember);
インスタンスのメンバを列挙する。 †for (var m in myObj) // m にはメンバ名が入る
{
document.writeln(m + " = " + myObj[m]);
}
with を使った名前空間もどき †myspace = {}; with(myspace) {
hello = function() {
alert("Hello, world!");
}
}
myspace.hello();
文末のセミコロンについて †
デバッガ †メモ †
|