オブジェクト作成ファクトリ

2041 ワード



    
        
        
        <script type="text/javascript">
            
            /*
             *       
             */
            var obj = {
                    name:"   ",
                    age:18,
                    gender:" ",
                    sayName:function(){
                        alert(this.name);
                    }
            };

            /*
             *           
             *                 
             */
            function createPerson(name , age ,gender){
                //         
                var obj = new Object();
                //        
                obj.name = name;
                obj.age = age;
                obj.gender = gender;
                obj.sayName = function(){
                    alert(this.name);
                };
                //       
                return obj;
            }
            
            /*
             *         
             */
            function createDog(name , age){
                var obj = new Object();
                obj.name = name;
                obj.age = age;
                obj.sayHello = function(){
                    alert("  ~~");
                };
                
                return obj;
            }
            
            var obj2 = createPerson("   ",28," ");
            var obj3 = createPerson("   ",16," ");
            var obj4 = createPerson("   ",18," ");
            /*
             *            ,         Object
             *           Object    ,
             *                     
             */
            //        
            var dog = createDog("  ",3);
            
            console.log(dog);
            console.log(obj4);
            
            
        </script>
    
    
    


</code></pre> 
</article>
                            </div>
                        </div>