zoukankan      html  css  js  c++  java
  • 学习篇之函数就是对象

        var person = new Object();
                person.name = "bob";
                person.age = 34;
                person.job = "Software";
    
                person.sayName = function(){
                    alert(this.name);
            }
    
            // 对象字面量写法
            var person = {
                name:"zhs",
                age:32,
                job:"Software",
                sayName:function(){
                    alert(this.name);
                }
            }
            
            // 工厂模式
            function creatPerson(name,age,job){
                var o = new Object();
                o.name = name;
                o.age = age;
                o.sayName = function(){
                    return this.name
                }
                return o;
            }
            var p1 = new creatPerson("bob","220","清洁工");
            console.log(p1);
            
            // 构造函数模式 (有点 可以判断对象类型)
            function Person(name,age,obj){
                this.name = name;
                this.age = age;
                this.obj = obj;
                this.sayName = function(){
                    // alert(this.name);
                }
            }
            var pa1 = new Person("sos",44,"软件工程师");
            var pa2 = new Person("ccc",22,"软件工程师");
            console.log(pa2.constructor == Person); // true 检测对象类型
            console.log(pa2 instanceof Person) // true
            var o = new Object();
            o = pa1;
            Person.call(o,"aaa",55,"sss");
            console.log(o.sayName());
    
            // 原型模式
            function Ps(){}
            Ps.prototype.name = "asd";
            Ps.prototype.age = 34;
            var ps1 = new Ps();
            console.log(ps1.age); //34
    
            // hasOwnPrototype() 原型属性返回 false,实例属性返回 true
            // in 包含原型属性返回 true
            var ps2 = new Ps();
            ps2.name = "cacaca";
            console.log(ps2.name);
            console.log(ps2.hasOwnProperty("name")); // true
            console.log("age" in ps2); // true
            

        // 判断原型属性 函数 function hasPrototyperty(object,name){ return !object.hasOwnProperty(name) && (name in object); } var ps3 = new Ps(); ps3.name = "aaac"; // 实例属性 console.log(hasPrototyperty(ps3,"name")); // false
  • 相关阅读:
    问题账户需求分析
    UnityWebRequest_ZT
    NetworkManager网络通讯_问题汇总(四)
    NetworkManager网络通讯_NetworkLobbyManager(三)
    NetworkManager网络通讯_NetworkManager(二)
    C#关于private protected sealed Virtual/Override
    NetworkManager网络通讯_Example(一)
    Udp 异步通信(三)
    TCP Socket服务端客户端(二)
    TCP UDP基本编程(一)
  • 原文地址:https://www.cnblogs.com/pangzi666/p/5627122.html
Copyright © 2011-2022 走看看