zoukankan      html  css  js  c++  java
  • javascript的类、委托、事件

    javascript中的类:


    function Person(name, age) {
                
    this._name = name;
                
    this._age = age;
                
    //对应Name的Get,Set方法,这个和Java的属性写法很像。
                this.getName = function() {
                    
    return this._name
                };
                
    this.setName = function(name) {
                    
    this._name = name;
                }
                
    //对应Age的Get,Set方法
                this.getAge = function() {
                    
    return this._age;
                }
                
    this.setAge = function(age) {
                    
    this._age = age;
                }
                
    //显示Person的信息方法
                this.show = function() {
                    alert(
    "Name:" + this.getName() + "; Age:" + this.getAge());
                }
            }

            
    //空构造方法
            var p1 = new Person();
            p1.setName(
    "Southsea");
            p1.setAge(
    23);
            p1.show();

            
    //带参的构造方法
            var p2 = new Person("Southsea"23);
            p2.show();
            
            
    //注:Javascript中没有真正的方法重载

    看起来很简单吧。

    下面我们把Pererson类的show方法加一个参数,让它具有委托的功能。


            function Person(name, age) {
                
    this._name = name;
                
    this._age = age;
                
    //对应Name的Get,Set方法,这个和Java的属性写法很像。
                this.getName = function() {
                    
    return this._name
                };
                
    this.setName = function(name) {
                    
    this._name = name;
                }
                
    //对应Age的Get,Set方法
                this.getAge = function() {
                    
    return this._age;
                }
                
    this.setAge = function(age) {
                    
    this._age = age;
                }
                
    //显示Person的信息方法
                this.show = function(delegate) {
                    
    if (delegate) {
                        
    delegate(this);
                    }
                }
    //只有这段与上面的不同。
            }

            
    //订阅Person类的show
            function showPerson(p) {
                alert(
    "Name:" + p.getName() + "; Age:" + p.getAge());
            }

            var p 
    = new Person("Southsea"23);
            p.show(showPerson); 
    //别写成p.show(showPerson());哦

    javascript中的事件


            function Person(name, age) {
                
    this._name = name;
                
    this._age = age;
                
    //对应Name的Get,Set方法,这个和Java的属性写法很像。
                this.getName = function() {
                    
    return this._name
                };
                
    this.setName = function(name) {
                    
    this._name = name;
                }
                
    //对应Age的Get,Set方法
                this.getAge = function() {
                    
    return this._age;
                }
                
    this.setAge = function(age) {
                    
    this._age = age;
                }
                
    this.onShow = null;//加了onshow事件
                
    //显示Person的信息方法
                this.show = function() {
                    
    if (this.onShow) {
                        
    this.onShow(this);
                    }
                }
            }

            
    //订阅Person类的show
            function showPerson(p) {
                alert(
    "Name:" + p.getName() + "; Age:" + p.getAge());
            }

            var p 
    = new Person("Southsea"23);
            p.onShow 
    = showPerson; //千万别写成p.onShow = showPerson();
            p.show(); 

    委托和事件都看起来很简单吧。

    javascript的动态类,它的格式是与JSON一样的。


            var person = {
                
    "Name""Southsea",
                
    "Age"23"show": function() {
                    alert(
    "Name:" + person.Name + "; Age:" + person.Age);
                }
            };
            person.show();
  • 相关阅读:
    zabbix 对/etc/ssh/sshd_config文件的监控 但status为unknowen
    Kotlin从零到精通Android开发
    谷歌官方 构建您的第一个应用 Kotlin版
    android studio 运行按钮为灰色的解决办法之一
    webapi发布到windows 2012的iis8里 出错
    Asp.net MVC WebApi项目的自动接口文档及测试功能打开方法
    Asp.net Web Api开发(第四篇)Help Page配置和扩展
    关于SNMP的MIB文件的语法简述
    Visual Stdio 2017增加SVN支持
    ffmpeg 多个音频合并 截取 拆分
  • 原文地址:https://www.cnblogs.com/BlogNetSpace/p/1501245.html
Copyright © 2011-2022 走看看