JavaScript_04

来源:互联网 发布:c语言temp怎么用 编辑:程序博客网 时间:2024/05/29 03:03

   一. 数组

      

<title>数组</title><script type="text/javascript">// 定义的方式// 1. var arr = new Array();arr[0] = "hello";arr[1] = 100;arr[2] = true;arr["a"] = 250; // 关联数组不占用元素长度// 可以存放任意类型的数据alert(arr.length); // 3// 2. 限定长度:长度还是可以变var arr2 = new Array(3);arr2[0] = 100;arr2[1] = "hello";arr2[2] = false;arr2[3] = function() {alert("哈哈");}// alert(arr2.length);// 3. 直接赋值var arr3 = new Array("hello", 250, false, function(){});// alert(arr3[3]);// 4. 字面量var arr4 = [];arr4[0] = 120;// alert(arr4[0]);// 5. 字面量直接赋值var arr5 = [120, "hello", false, function(){ alert("ok")}];// alert(arr5.length);// delete 操作运算符  删除元素/属性delete arr5[3]; // 元素删除,但是空间依旧占用 undefinedalert(arr5.lenght)for(var i = 0; i < arr5.length; i++) {// alert(arr5[i]);}// 多维数组:二维数组var arr6 = [[133,20], 120, 30];for(var j = 0; j < arr6.length; j++) {if(typeof arr6[j] == "object") { // 判断是否是数组for(var x = 0; x <arr6[j].length; x++) {// console.log(arr6[j][x]);}} else {// console.log(arr6[j]);}}for(var index in arr5) {console.log(index + " := " + arr5[index]);}// 设计一个返回数组长度的方法? 添加一个方法       ie9 以下没效果Array.prototype.getSize = function() {return this.length;}// alert(arr6.getSize());</script></head><body></body></html>

        1.关联数组不占用元素长度

        2. 限定长度:长度还是可以变
        3. 直接赋值
        4. 字面量
        5. 字面量直接赋值

        6.数组的常用方法


   二. call以及apply实现继承

<title>call 以及 apply 实现继承</title><script type="text/javascript">/*  */function fn(width, height) {this.width = width;this.height = height;// area(width, height)}function area(width, height) {alert(width*height);}var a = new fn(10, 10);// area.call(fn, a.width, a.height);/* area.apply(fn, [a.width, a.height]); */function Person(name, age) {this.name = name;this.age = age;this.eat = function() {alert(this.name + " == " + this.age);}}// 类function chinese(name, age) {// Person.call(this, name, age);Person.apply(this, [name, age]);// 成员方法this.sleep = function() {alert(this.name + " like sleeping");}/* this.eat = function() {alert("我是重写的方法。。");} */}var ch = new chinese("张三", 20);ch.eat();ch.sleep();</script></head><body></body></html>

    三.prototype

     

<script type="text/javascript">function Animal(name, age) {this.name = name;this.age = age;this.eat = function () {alert(this.name + " == is eatting...");}this.bite = function() {alert(this.name + " == " + this.age);}// 在函数内定义方法时候,如果有重名的,使用原型定义时还是以非原型的为主Animal.prototype.bite = function() {alert(this.name + " == can bite human");}}// 在函数外部添加方法Animal.prototype.out = function() {this.eat();alert(this.name + " == is getting out");}// 在函数外部添加属性Animal.prototype.sex = "雌性";var animal = new Animal("阿黄", 20);/* animal.eat();animal.bite(); */animal.out();alert(animal.sex);/* 1. 可以给函数添加额外的方法或者属性 2. 可以类似于重写函数的方法 */</script></head><body></body></html>


    四.混合方式实现继承

      

<title>混合方式实现继承</title><script type="text/javascript">/* call(apply) + 原型的方法 */function Person(name, age) {this.name = name;this.age = age;}// 在外部使用原型定义方法的时候,同 call/apply 是继承不了的Person.prototype.eat = function() {alert(this.name + " like eattin");} // 继承function chinese(name, age) {// Person.call(this, name, age);Person.apply(this,[name, age]);this.sleep = function() {alert("ok");}}chinese.prototype = new Person(); // 指定原型ch = new chinese("张三", 20);ch.eat();ch.sleep();</script>


    五.继承一

   

<title>对象冒充</title><script type="text/javascript">// 对象冒充方式实现继承function Animal(name, age) {this.name = name;this.age = age;this.eat = function() {alert(this.name + " == " + this.age);}}function Dog(name, age, sex) {// 定义一个属性this.animal = Animal;// 类似于 Animal(name, age);this.animal(name, age);// delete 删除对象里面的一个属性或者数组的一个元素delete animal;// 初始化自己的属性this.sex = sex;// 子类特有的this.bite = function() {alert(this.name + " == " + this.age)}}var dog = new Dog("阿黄", 20, "雌性");dog.eat();dog.bite();</script></head><body></body></html>


    

原创粉丝点击