Front-end Technology (JavaScript) Study Notes (2) Custom Objects and Exception Handling in JavaScript

0. JavaScript objects, objects in js are regarded as containers of properties, that is, objects in js contain many properties. It should be noted that objects (classes) in java may have properties (variables) and methods. In fact, in addition to the related properties, there are also methods in the js object, but for Javascript, the methods in the object are also stored as properties.

1. Custom object syntax in javascript 1. The specific situation is as follows: the code and the comment statement in it

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript">
//          定义employee是一个对象,换言之,即是在javascript中的去创建对象   通过 new关键字创建对象
//          在javascript中可以先去创建对象,然后再向对象中添加属性和方法
            var employee =new Object();
//          给employee对象添加一个empno这个属性,其值是8899;
            employee.empno=8899;
//          给employee对象添加一个属性ename,其值刘备
            employee.ename="刘备";
//          js中对象中的方法是以属性存储的,所以如下代码是给employee对象添加一个属性print,但是这个属性的值确实一个方法,所以print即是一个方法了
            employee.print=function(){
                document.write(employee.empno+"---&gt;"+employee.ename);
            };//注意这里有一个分号,就像一条定义变量的语句一样,var a=10;。所以这里必须有一个分号
//          为employee对象添加toS方法,方法的功能是在页面画出当前对象empno值和name值
            employee.toS=function(){
                document.write(employee.empno+"&lt;---&gt;"+employee.ename)
            }
            /*
            document.write(typeof employee);
            对象名.属性名访问对象中的属性
            document.write(employee.empno);
            document.write(employee.ename);
            */
//          方法调用,直接通过对象名.方法名调用即可
            employee.print();
            employee.toS();
        </script>
    </head>
    <body>
    </body>
</html>

2. Custom object syntax in javascript 2. The specific situation is as follows: the code and the comment statement in it

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/study.js"></script>
        <script type="text/javascript">
//          JavaScript中的对象是属性的容器
            var name="刘备";
//          在JavaScript中定义对象  对象名User
            var User={
                userid:"liubei",
                password:"admin",
                address:"三国",
//              注意:在这种对象的创建方法中,对象中的方法也是以属性形式储存的.
                show:function(){
                    document.write("show method is running......");
                }
            };
//          访问对象中的属性  对象名.属性名;或者对象名["属性名"]
//          document.write(User.password+"&nbsp;"+User.userid+"&nbsp;"+User["address"]);
        </script>
    </head>
    <body>
    </body>
</html>

Experiment: Define the case code and job in the previous section in the object The
reference code is as follows:

var MyMath={
    auther:"Rock",
    createTime:"20180425",
    jiShuHe:function(min,max){
        var sum=0;
        for(var i=min;i<=max;i++){
            if(i%2!=0)
                sum+=i;
        }
        /*
        因为JavaScript是若类型的编程语言,所有在定义方法时无需指定方法的返回值,若希望给调用者一个结果数据的话
        可以在合适的地方直接通过return返回结果数据即可
        */
        return sum;
    },
    printKouJue:function(){
        for(var i=1;i<10;i++){
            for(var j=1;j<=i;j++){
                var result=j*i;
                if(result<10&& j!=1)
                    result="&nbsp;&nbsp;"+result;
                document.write(j+"*"+i+"="+result);
                document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            }
            document.write("<br/>");
        }
    }
};

3. Exception handling in JavaScript
Syntax :
try{
code block where errors may occur//Throw error information to the outside through throw, and this error information will be stored in the err of catch. Of course, if there is no error in the code in the try body, then the code in the catch body will not be executed
}
catch(err){
code block for exception handling
}

case code

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript">
            function testException(legs){
                try{
                    if(legs<0) throw "动物腿的数量不能为负值";
                    if(legs>1000){
                        throw "动物腿的数量能有那么多吗?";
                    }
                    else throw "动物腿的数量合法";
                }
                catch(err){
                    document.write(err);
                }
            }
            testException(-10);
        </script>
    </head>
    <body>
        <span></span>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript">
            function messsage(){
                try{
                    gout();//注意这个方法是不存在的,所以在调用这个方法时会抛出一个错误
                }
                catch(err){
                    alert("方法不存在");
                }
            }
            messsage();
        </script>
    </head>
    <body>
    </body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939610&siteId=291194637