標籤: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.()); //Regex 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=‘[email protected]‘; 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函數物件導向筆記