zoukankan      html  css  js  c++  java
  • JS语法学习笔记

    JS语法:


    JS知识点一览图

    JS知识点一览图

    在function中写this,在本function中可以显示,写Person则显示undefined.
    在function中写Person,在function外可以显示,写this则显示undefined.
    var name;
    var age;
    var info;
    function Person(name,age){
    this.name=name;
    Person.age=age;
    this.info=function(){
    document.writeln("name is:"+this.name+"<br/>");
    document.writeln("age is:"+this.age+"<br/>");       
    };      
    }
    var p=new Person('chen',22);
    p.info();
    document.writeln("w-name is:"+this.name+"<br/>");
    document.writeln("w-age is:"+Person.age+"<br/>");
    
    用call方法遍历数组:
    var each=function(array,fn){    
        for(var index in array){
            fn.call(window,index,array[index]);
        }       
    }
    each([4,20,3],function(index,ele){
        document.write("第"+index+"个元素是:"+ele+"<br/>");
    });
    
    全局变量在函数里用this取不到,要用window调用。
        var scope="全局变量" ;
    
        function test(){
    
            document.writeln(scope+"<br/>");
            document.writeln(window.scope+"<br/>");
            var scope="局部变量";
            document.writeln(scope+"<br/>");
        }
        test();
    
    如果定义的函数没有明确赋值给某个对象,则用window调用 。
    var hello=function(name){
        document.write(name+"say:hello<br/>");      
    }
    window.hello("chen");
    
    继承
    function Base(){
        this.member="em";
        this.method=function(){
            window.alert(this.member);
        }   
    }
    function extend(){
        Base.call(this);
        window.alert(member);
        window.alert(this.method);      
    }
    extend();
    
    继承进阶:输出的是Cat,执行的是Animal里的showName。
    function Animal(){
        this.name='Animal';
        this.title='AnimalTitle';
        this.showName=function(){
            alert(this.name);
        }
    }
    function Cat(){
        this.name='Cat';
        this.showName=function(){
            alert('cat'+this.name); 
        }   
    }
    var animal=new Animal();
    var cat=new Cat();
    animal.showName.call(cat);  
    cat.title='catTitle';
    console.log(cat.title);
    
    call() 和 apply()的区别:

    call方法调用时,必须在括号中详细地列出每个参数,参数可以是任意类型(参数不限数量)。 apply方法调用时,可在括号中以arguments 或 数组 来代表所有参数(参数只能有两个)。

    它们各自的定义:

        apply:应用某一对象的一个方法,用另一个对象替换当前对象。例如:B.apply(A, arguments);即A对象应用B对象的方法。

        call:调用一个对象的一个方法,以另一个对象替换当前对象。例如:B.call(A, args1,args2);即A对象调用B对象的方法。

      它们的共同之处:

        都“可以用来代替另一个对象调用一个方法,将一个函数的对象上下文从初始的上下文改变为由thisObj指定的新对象”。

      它们的不同之处:

        apply:最多只能有两个参数——新this对象和一个数组argArray。如果给该方法传递多个参数,则把参数都写进这个数组里面,当然,即使只有一个参数,也要写进数组里。如果argArray不是一个有效的数组或arguments对象,那么将导致一个TypeError。如果没有提供argArray和thisObj任何一个参数,那么Global对象将被用作thisObj,并且无法被传递任何参数。

        call:它可以接受多个参数,第一个参数与apply一样,后面则是一串参数列表。这个方法主要用在js对象各方法相互调用的时候,使当前this实例指针保持一致,或者在特殊情况下需要改变this指针。如果没有提供thisObj参数,那么 Global 对象被用作thisObj。

        实际上,apply和call的功能是一样的,只是传入的参数列表形式不同。

    prototype.constructor:

    a) Prototype:每一个函数都包含一个prototype属性,这个属性指向的是一个对象的引用;而对已每一个函数(类)的实例都会从prototype属性指向的对象上继承属性,换句话说通过同一个函数创建的所有对象都继承一个相同的对象。

    b) 通过new 关键字和构造函数创建的对象的原型,就是构造函数的prototype指向的那个对象

    每一个函数的Prototype属性指向的对象都包含唯一一个不可枚举属性constructor,它指向了它所在的构造函数。

    arguments

    在JavaScript中,arguments对象是比较特别的一个对象,实际上是当前函数的一个内置属性。arguments非常类似Array,但实际上又不是一个Array实例。

    innerHTML

    用来获取对象的内容(还可以向对象插入内容) 这里输出的是Firstname、Lastname。

    <script type="text/javascript">
    function getInnerHTML(){
        alert(document.getElementById("tr1").innerHTML);        
    }    
    </script>
        <table border="1" id="t1">
            <tr id="tr1">
                <th>Firstname</th>
                <th>Lastname</th>
            </tr>
            <tr id="tr2">
                <td>Peter</td>
                <td>Griffin</td>
            </tr>
        </table>
        <br />
        <input type="button" onclick="getInnerHTML()"
            value="Alert innerHTML of table row" />
    
    遍历对象的属性
    function Person(name,age){
        this.name=name;
        this.age=age;
        this.info=function(){
            alert('info method');
        }   
    }
    var p=new Person('chen',22);
    for(a in p){
        document.writeln(p[a]); 
    }
    
    prototype
    function Person(name,age){
        this.name=name;
        this.age=age;
        this.info=function(){
            document.writeln(this.name);
            document.writeln(this.age);
        }   
    }
    var p1=new Person('chen',22);
    p1.info();
    Person.prototype.walk=function(){
        document.writeln(this.name+'walk...');  
    }
    var p2=new Person('zhi',23);
    p2.info();
    p2.walk();// zhiwalk... 
    p1.walk();// chenwalk...
    

    用这句话把walk方法嵌进去,这样Person类就有了walk方法,并且p1也能调用了。而且不污染代码。

    Person.prototype.walk=function(){
        document.writeln(this.name+'walk...');  
    }
    

    并不是建立一个新类,是对原类进行修改。所以称为伪继承。既可以扩展自定义类,也可以扩展JS内部对象。

  • 相关阅读:
    ABP 开发(一)
    转发: Angular装饰器
    AJAX上传文件到服务器
    转:system.Security.Cryptography C# 加密和解密
    asp.net mvc5 step by step(四)——关于Controller的ActionResult
    asp.net mvc5 step by step(三)—— Entity Framework Database First
    asp.net mvc5 step by step(二)——Data Annotations(data 注释)
    项目中使用protobuf
    架构私用Nuget服务器
    dockerfile语法
  • 原文地址:https://www.cnblogs.com/CZDblog/p/8609317.html
Copyright © 2011-2022 走看看