Javascript基础知识总结一

Javascript基础知识总结一

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        /*JavaScript词法结构*/
        var a = Math.random();
        console.log("随机值:" + a);
        //返回最大值
        var m = Math.max(1, 2, 3);
        console.log(m);
        //  javascript  Date
        var d = new Date();
        var m = d.getFullYear();
        console.log(m);
        //instanceof运算符表示如果左侧的对象是右侧的实例则返回  true
        console.log(d instanceof Date);
        // delete 运算符:
        var o = {x: 1, y: 2};
        console.log("删除前:");
        console.log(o);
        console.log(delete o.x);
        console.log("删除后:");
        console.log(o)
        //异常处理:
        try {

        } catch (exception) {

        }
        finally {

        }
        //javascript oop:
        /*首先简单认识下面向对象都有哪些特性:参考:http://www.ibm.com/developerworks/cn/web/1304_zengyz_jsoo/
         * 1:一切事物皆对象  2:对象具有封装、继承、多态  3:对象与对象之间通过消息进行通信
         * javascript 基于原型(prototype) 与基于类(Class)的 oop
         * */
        //使用原型链实现继承:
        //声明对象构造器(constructor):
        function Animal() {

        }
        //指定animal对象的原型:
        Animal.prototype = {
            name: "animal",
            weight: 0,
            des: function () {
                console.log("动物")
            }
        };
        //声明Mammal(哺乳動物)的对象构造器(constructor)
        function Mammal() {
            this.name = "mammal"
        }
        //创建对象mammal和对象animal之间的原型链:
        Mammal.prototype = new Animal();
        //声明sheep对象构造器:
        function Sheep(height, weight) {
            this.name = "sheep",
                    this.weight = weight,
                    this.height = height
        }
        //构建sheep与mammal之间的原型链:
        Sheep.prototype = new Mammal();
        //重新指定des方法:此方法将覆盖从animal继承过来的des方法
        Sheep.prototype.des = function () {
            console.log("羊")
        }
        //验证:
        var sheep = new Sheep(200, 100)
        //每个对象都有一个 __proto__属性来实现对原型的隐式引用
        console.log(sheep.__proto__ === Sheep.prototype)  //--true
        console.log(Sheep.prototype.__proto__ === Mammal.prototype)  //--true
        console.log(Mammal.prototype.__proto__ === Animal.prototype)  //--true
        sheep.des();


        //JavaScript类式继承实现方法:
        function Sup() {
            this.colors = ["red", "blue"];
        }
        function Sub() {
            Sup.call(this);
        }
        console.log(new Sub().colors[0]);
        //javascript 利用闭包实现信息隐藏
        function user(name) {
            //定義私有屬性
            var _name = name;
            //定義私有方法
            function getName() {
                return _name;
            }

            //讓該對象的其他公共方法能訪問到私有成員
            this.nameService = function () {
                return getName();
            }
        }
        //聲明公共成員:
        user.prototype.pubGetName = function (name) {
            return this.nameService() === name;
        }
        var u = new user("哆啦A夢");
        console.log(u.pubGetName("哆啦A夢"));
        console.log(u.name);  //undefined,表示無法訪問私有成員
        console.log(u.nameService())

        /*JavaScript 函數 1:定義函數時第一個字母小寫
         * */
        //定義一個匿名函數
        $(function () {
            (function (x, y) {
                console.log(x + y);
            })(2, 3);//并立即執行:
        })
    </script>
</head>
<body>
<header>jascript基础学习一</header>

</body>
</html>

  未完待续...

posted @ 2015-10-13 16:39  大白的记事本  阅读(475)  评论(0编辑  收藏  举报