zoukankan      html  css  js  c++  java
  • JS函数的原型及对象,对象方法,对象属性的学习

    看一个简单的例子:

    <script type="text/javascript">
            // 以下是对函数,对象,对象方法,对象属性及原型作一个简单的实例说明:
            var test = test1 = test2 = function (width, height) { //同时建立多个函数(指定同一个对象的对象方法的实例)
                return new test.fn.init(width, height);
            };
            test.prototype = test.fn = {
                init: function (width, height) { alert("作者:" + this.Author + "宽为:" + width + ",高为:" + height); },//对象方法(函数)
                Author: "张占岭(Lose)" //对象属性
            };
     
            test(800, 600);
            test1(400, 300);
          
        </script>

    再来看一个访jquery的选择器是如何使用它们的:

    <script type="text/javascript">
     
     
     
            // Jquery的选择器是怎么炼成的.
            // 作者:张占岭
            // 相关知识点:JS对象,原型,JS正则表达式,对象方法,对象属性等
            // 定义变量 undefined 方便使用
            var zzlQuery = window.zzlQuery = function (selector, context) { //定义一个函数zzlQuery
                return new zzlQuery.fn.init(selector, context); //函数返回一个对象
            },
            toString = Object.prototype.toString, //object 等于object的原型
             idExpr = /^#([\w-]+)$/; //查询是否为一个ID
            // 设置 jQuery 的原型对象, 用于所有 jQuery 对象共享
            zzlQuery.fn = zzlQuery.prototype = {                        // #74
                length: 0,                                          // #190
                version: "1.0.0",                                    // # 187
                // 这是一个示例,仅仅提供两种选择方式:id 和标记名
                init: function (selector, context) {                // #75
                    // Handle HTML strings
                    if (typeof selector === "string") {
                        // Are we dealing with HTML string or an ID?
                        match = idExpr.exec(selector); //执行一个正则表达式
                        // Verify a match, and that no context was specified for #id
                        if (match && match[1]) {
                            var elem = document.getElementById(match[1]);
                            if (elem) {
                                this.length = 1;
                                this[0] = elem;
                            }
                        }
                        else {
                            // 直接使用标记名
                            var nodes = document.getElementsByTagName(selector);
                            for (var l = nodes.length, j = 0; j < l; j++) {
                                this[j] = nodes[j];
                            }
                            this.length = nodes.length;
                        }
                        this.context = document;
                        this.selector = selector;
                        return this;
                    }
                },
     
                // 代表的 DOM 对象的个数
                size: function () {
                    return this.length;
                },
                // 用来设置 css 样式
                css: function (name, value) {
                    this.each(
                        function (name, value) {
                            this.style[name] = value;
                        },
                        arguments       // 实际的参数以数组的形式传递
                        );
                    return this;
                },
                // 用来设置文本内容
                text: function (val) {
                    if (val) {
                        this.each(function () {
                            this.innerHTML = val;
                        },
                        arguments       // 实际的参数以数组的形式传递
                        )
                    }
                    return this;
                },
                // 用来对所有的 DOM 对象进行操作
                // callback 自定义的回调函数
                // args 自定义的参数
                each: function (callback, args) {                   // #244
                    return zzlQuery.each(this, callback, args);
                }
            }
            // init 函数的原型也就是 jQuery 的原型
            zzlQuery.fn.init.prototype = zzlQuery.prototype;            // #303
            // 用来遍历 jQuery 对象中包含的元素
            zzlQuery.each = function (object, callback, args) {       // #550
                var i = 0, length = object.length;
                // 没有提供参数
                if (args === undefined) {
                    for (var value = object[0];
                        i < length && callback.call(value, i, value) !== false;
                         value = object[++i])
                    { }
                }
                else {
                    for (; i < length; ) {
                        if (callback.apply(object[i++], args) === false) {
                            break;
                        }
                    }
                }
            }
            zzlQuery("h2").text("Hello, world.").css("color", "red");  //调用涵数
     
     
        </script>

    看完之后是不是你也想说:哇,原来JS也能面向对象呀!

  • 相关阅读:
    mac 终端命令
    安装和使用Carthage
    Mac下的常用终端命令与vim常用命令
    swift 获取推送deviceToken
    使mac版的MYSQL支持emoji表情
    iOS --- 通过CPU实现的简单滤镜效果
    IOS多选单选相册图片
    AVCaptureDevice iOS摄像头属性
    GPUImage 滤镜介绍
    苹果IOS开发者账号如何续费-Appstore
  • 原文地址:https://www.cnblogs.com/lori/p/2089172.html
Copyright © 2011-2022 走看看