`
jiav_net
  • 浏览: 104418 次
文章分类
社区版块
存档分类
最新评论

JS学习点滴1

 
阅读更多

1. for in 语句

用来遍历对象的属性和方法

1.1 通过动态方式创建的对象
var obj = new Object();
		obj.name = "zhanghui";
		obj.print = function () {
			alert(this.name);
		};
		for (p in obj) {
			alert(p); // name; print
		}

1.2 通过类的方式创建的对象
function Father (name) {
   this.name1 = name;
   this.print1 = function () {
    alert("print1(): " + this.name1 + " " + this.name2);
   };
  }
  Father.prototype.name2 = "name2";
  Father.prototype.print2 = function () {
   alert("print2(): " + this.name1 + " " + this.name2);
  };

  var f1 = new Father("name1");
  for (var p in f1) { // 对一个对象进行遍历.包括Father通过this和prototype赋予的所有属性和方法
   alert(p); // name1; print1; name2; print2
  }
  for (var p in Father) { // 不能使用类名
   alert(p); // nothing
  }
  for (var p in Father.prototype) { // 只包含Father通过prototype赋予的属性和方法
   alert(p); // name2; print2
  }

2. with语句

其实和java的静态导入差不多

var s = "hello world";
		with(s){
			alert(toUpperCase());
		}

3. switch语句可以判断字符串

var s = "xyz";
		switch(s){
			case "xyz" : 
				alert(s);
				break;
		}

4. delete运算符

删除对象的属性或方法

var obj = new Object();
		obj.name = "allei";
		obj.print = function () {
			alert(this.name);
		};
		
		alert(obj.name);
		alert(obj.print); // 打印函数体
		
		delete obj.name;
		alert(obj.name); // undefined
		delete obj.print;
		alert(obj.print); // undefined

5. 变量可以不用先声明即可使用,不过不建议这么做,程序可读性不好

 var s1 = "abc";
        s2 = s1 + "xyz";
        alert(s2); // abcxyz

6. typeof运算符

// 下面几个变量存放在栈中
var s = "abc";
  var b = false;
  var f = 1.2;
  var i = 100;
  var u;
  alert("typeof(s): " + typeof(s) + "\n" + // string
  "typeof(b): " + typeof(b) + "\n" + // boolean
  "typeof(f): " + typeof(f) + "\n" + // number
  "typeof(i): " + typeof(i) + "\n" + // number
  "typeof(u): " + typeof(u) + "\n"); // undefined

  // 下面几个变量存放在堆中 
  s = new String("abc");
  b = new Boolean(false);
  f = new Number(1.2);
  i = new Number(100);
  var o = new Object();
 var date = new Date();
 var myfun1 = new Function ();
 var myfun2 = function () {}
  alert("typeof(s): " + typeof(s) + "\n" + // object
 "typeof(b): " + typeof(b) + "\n" + // object
 "typeof(f): " + typeof(f) + "\n" + // object
 "typeof(i): " + typeof(i) + "\n" + // object
 "typeof(o): " + typeof(o) + "\n" + // object
 "typeof(date): " + typeof(date) + "\n" + // object
 "typeof(myfun1): " + typeof(myfun1) + "\n" + // function
 "typeof(myfun2): " + typeof(myfun2) + "\n" // function
 ); 

7. 基本类型和引用类型

JS的变量要么放在栈中,要么放在堆中。放在栈中的是基本类型,放在堆中的是引用类型(引用本身存放在栈中,类似于c++的指针)。

为变量赋值时,JS引擎必须判断该值是原始类型还是引用类型。

基本类型5种:Null,Undefined,Boolean,Number,String,存放在栈(stack)中。


8. undefined

表示变量已经定义,当尚未初始化。

 // undefined 从null派生而来
        alert(null == undefined); // true
        
        
        // 当函数无明确返回值时,实际上返回undefined
        function fun6 () {
            return;
        }
        function fun7() {
            // nothing
        }
        
        alert(typeof fun6()); // undefined
        alert(typeof fun7()); // undefined
        alert(fun6() == undefined); // true
        alert(fun7() == undefined); // true

9.函数

JS里面的函数,你可以把它当做普通的函数,定义,然后调用;也可以把他看做一个构造函数,然后尽情的使用this.xxx。

// 1		
		function fun (userName, password) {
			alert(userName + " " + password);
		}
		//fun("allei", "123456");
		
		// 2
		var fun2 = new Function ("userName", "password", "alert(userName + \" \" + password);") ;
		//fun2("allei", "123456");
		
		// 3
		var fun3 = function (userName, password) {
			alert(userName + " " + password);
		};
		//fun3("allei", "123456");
		
		// 4.arguments对象
		function fun4 (userName, password) {
			alert(arguments.length);
		}
		//fun4("allei", "123456");
		
		// 调用函数时参数的个数和函数定义的个数可以不同。多的会被忽略,少得会填以undefined
		function fun5 (userName, password) {
			alert(userName + " " + password + " "  + arguments[2]);
		}
		//fun5("allei", "123456", "the 3rd argument");
		//fun5("allei"); // "allei undefined undefined"
		
		// 5. length属性,表示定义的参数个数,不是实际调用时传递的参数个数
		//alert(fun5.length); 





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics