码迷,mamicode.com
首页 > 编程语言 > 详细

javacript函数面向对象笔记

时间:2017-02-09 19:25:12      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:create   back   字符   ceo   ...   枚举   ret   reg   price   

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script type="text/javascript">
		     function hero(name) {            //构造函数
            this.name=name;
            this.occupation=‘Ninja‘;
            this.whoAreyou=function() {
                 return ‘I am‘+ this.name +this.occupation;
            }
              
         }

         var hero1=new hero(‘Michelangelo‘);
          console.log(hero1.name);
         var hero2=new hero(‘Lucy‘);
           console.log(hero2.name);
           console.log(window.name);


           var obj={};
            console.log(obj.constructor);
            console.log(typeof obj.constructor);

           //  instanceof  检测一个对象是不是有某个指定的构造函数所创建的;
           function Hero( ) {  };
            var hero_1=new Hero();
            var obj_a={};
           console.log(hero_1 instanceof Hero);
            console.log(hero_1 instanceof Object);
            console.log(obj_a instanceof Object);

            var  first_a=new Function(‘a‘,‘b‘,‘c‘,‘d‘, ‘return arguments‘ );
            console.log(first_a(1,2,3,4));
            var first_b=new Function(‘a‘,‘b‘,‘c‘,‘d‘, ‘return arguments‘ );
            console.log(first_b(12,15,13,14));
            var first_c=new Function(‘a‘,‘b‘,‘c‘,‘d‘, ‘return arguments‘ );
            console.log(first_c(4,5,6,7));

              var some_obj={
                  name:‘Ninja‘,
                  say:function(){
                    return  "I‘m a" + this.name;
                  }
              }

              function some_fun() { }

               some_fun.prototype=some_obj;
               console.log(some_fun.name)
               var some_Ninja=new some_fun();
               console.log(some_Ninja.name);

               var some_obja={
                  name:‘Ninja‘,
                  say:function (who) {
                      return ‘haha ‘+ who +‘ I am a ‘+ this.name;
                  }
               }

               var mike={name: ‘Kate‘}

               console.log(some_obja.say(‘Jim‘));

              
               console.log( some_obja.say.call(mike,‘Ninja‘));
               console.log(some_obja.say.apply(mike,[‘Lily‘]));   //apply 参数通过数组传递

               var dateNow=new Date();
               console.log(dateNow);
                var dateM= dateNow.setMonth(2);
               console.log(dateM);
               dateNow.setMonth(3);
               console.log(dateNow.toString());
              var dateP=Date.parse(‘Jul 1,2013‘)
               console.log(dateP)
               //console.log(dateP.());

               //正则表达式
                var re= new RegExp("j.*t");
                console.log(re.test("javascript"));
               var s=new String(‘HelloJavascriptWorldstring‘);
                var sMatch= s.match(/a/g);   //匹配字符串的a的数组
               console.log(sMatch);
               var sSearch=s.search(/a/i);   //匹配字符串的a的位置
                console.log(sSearch);
              var sReplace=s.replace(/[A-Z]/g,‘ ‘);  //替换所有a-z大写字母,
                console.log(sReplace);

                var deEmial=‘deveil@163.com‘;
                var re=/(.*)@(.*)\.(.*)/;
                 var callbackfun=function () {
                    glob=arguments;
                    return arguments[1] +‘ at ‘+arguments[2]+‘ dot ‘+arguments[3];
                 }
                  console.log(deEmial.replace(re,callbackfun));

                 function fun_c() {
                     function fun_d() {
                          return this;
                          console.log(this);
                      }
                     fun_d();

                    }

                 var fun_a=new fun_c();

                 var arr_c=[1,2,12,[4,5,6]];
                console.log(arr_c.sort());
                console.log(arr_c.join(‘--‘));
                console.log(arr_c);

                //原型对象
                function Creatpro(name,color){
                     this.name=name;
                     console.log(this);
                     this.color=color;
                     this.whatareyou=function(){
                         console.log(‘iam ‘+this.name+this.color);     
                     };
                }
                
                  
              Creatpro.prototype={
                     price:100,
                     rating:3,
                     getInfo:function () {
                        console.log(‘Rating ‘+ this.rating+‘ ,price ‘+this.price);
                     }
                };
                 var CreatePro_a=new Creatpro(‘Lily‘,‘red‘);
                      CreatePro_a.whatareyou();
                      CreatePro_a.getInfo();
                      console.log(CreatePro_a.constructor);
                      console.log(CreatePro_a.constructor.prototype.rating);
                      console.log(CreatePro_a.name);
                      console.log(CreatePro_a.price);

                function getGarde(name) {
                         this.name=name;
                         
                      };
                    getGarde.prototype.name="person";     //设置默认属性name名称 
                var toy=new getGarde(‘robot‘);           // 新建一个对象 toy
                delete toy.name;                        //删除toy的名称
                console.log(toy.name)
                  toy.name=‘vegetables‘;              //设置toy的名称
                console.log(toy.name)


                function getGarde_a(name,color) {
                     this.name=name;
                     this.color=color;
                     this.say=function(){
                        console.log(this.name+‘ haha ‘+this.color )
                     }
                  // body...
                }
                getGarde_a.prototype.price=210;
                getGarde_a.prototype.rating=5;

                var getGarde_b={
                    tall:145,
                    animate:‘2d‘,
                    numbers:14
                 }
                getGarde_a.prototype=getGarde_b;
                console.log(getGarde_b.isPrototypeOf(‘getGarde_a‘));   //  判断getGarde_a的原型属性是不是getGarde_b的
                var toy_a=new getGarde_a(‘Lucy‘,‘orange‘);

                console.log(toy_a.name);

                for(var pop in toy_a){                     //遍历对象的所有属性方法
                    console.log(pop +‘ = ‘+ toy_a[pop]);
                }


                console.log(toy_a.propertyIsEnumerable(‘name‘)); // 属性是否可枚举   原型链的属性也是不可枚举的
                console.log(toy_a.propertyIsEnumerable(‘widths‘));
                console.log(toy_a.propertyIsEnumerable(‘rating‘));
                console.log(toy_a.hasOwnProperty(‘name‘));  //判断属性是原型对象 还是自身的;
                console.log(toy_a.hasOwnProperty(‘price‘));


                 

                function shape(typedd){ 
                    this.type=typedd;
                    this.getTypedd=function(){
                         console.log(this.type);
                    }
                   
                }
                var  shape_a= new shape(‘hello‘);
                 //console.log(shape_a.getTypedd());

                 function tpro(){
                    this.textd=‘javascript‘;
                    this.heights=1245;
                    this.widths=1246;
                    this.sayWidth=function(){
                        console.log(this.widths);
                    }
                 }
                 var tpro_a= new tpro();
                 for(porp in tpro_a){
                     console.log(porp +‘ = ‘ + tpro_a[porp]);
                 }


                 function shape_A() {
                    shape_A.prototype.name=‘shape_A‘;
                     shape_A.prototype.toString=function(){
                        return this.name;
                    }
                 }

                 function shape_B() {
                    this.name="2dshape";
                 }

                 function Triangle(side,height) {
                    this.name="Triangle";
                    this.side=side;
                    this.height=height;
                    this.getArea=function ( ) {
                        return  this.side*this.height/2;
                    }
                    
                 }
                  
                  shape_B.prototype= new shape_A();   //继承原型里的属性
                  Triangle.prototype= new shape_B();
                  shape_B.prototype.constructor=shape_B;
                  Triangle.prototype.constructor=Triangle;
                  var mytri= new Triangle(3,5);
                  console.log(mytri.getArea());
                  console.log(mytri.toString());

               //临时构造器

               function shapeCreate() {
                  shapeCreate.prototype.name="Java";
                  shapeCreate.prototype.showName=function () {
                       console.log(this.name);
                  }
               }
                 function shapeTwo(){};
                 var F=function () {  };

                 F.prototype=new shapeCreate();
                 shapeTwo.prototype=new F();
                 shapeTwo.prototype.constructor=shapeTwo;
                 shapeTwo.prototype.name="12121";
                 shapeTwo.prototype.showName();


    


           
              

	</script>
          
</head>
<body>

</body>
</html>

  

javacript函数面向对象笔记

标签:create   back   字符   ceo   ...   枚举   ret   reg   price   

原文地址:http://www.cnblogs.com/deveil/p/6383439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!