现在的位置: 首页 > 综合 > 正文

javascript示例代码

2012年10月21日 ⁄ 综合 ⁄ 共 4145字 ⁄ 字号 评论关闭

在javascript中创建并使用对象

<script>
var person = new Object();
person.name 
= "lgx";
person.sex 
= "man";
person.age 
= 24;
person.sayhello 
= function ()
{
    alert(
"hello my name is"+this.name);
}
</script>

<body>
<script>

person.sayhello();
</script></body>

使用JSON(javascript Object Notation)

  <script type=text/javascript>
  
var person={
   name:
"lgx",
   sex:
"man",
   age:
21,
   sayHello:
function(word){
    alert(word
+this.name);
   }

  }

  
</script>

 

    <form id="Form1" method="post" runat="server">
            
<FONT face="宋体"></FONT>
            
<script type=text/javascript>
            //person.sayHello('haha:');
            
</script>
            
        
</form>

定义复杂对象

var myLibrary=
{
    location:
"my house",
    keywords:[
"row vegetables","turnip","tedium"],
    books:[
    {title:
"The title",
     authors:[
       {name:
"jim brow",age:18},
       {name:
"xxxx",age:13}
     ]
    }
    ]
}

使用prototype声明对象属性和方法

function MyObject(name,size)
{
    
this.name = name;
    
this.size = size;
}
MyObject.prototype.tellsize 
= function()
{
    alert(
"size of"+this.name+"is"+this.size);
}
var myObject = new MyObject("tiddles","7.5 meters");
myObject.tellsize();

需要注意代码执行顺序,只有在构造函数声明之后才能引用其prototype属性,而且
对象从prototype所获得的属性和方法,必须声明在调用构造函数创建对象之前。

用prorotype实现继承

function Vehicle()
{
}
Vehicle.prototype.wheelCount 
=4;
Vehicle.prototype.curbWeightInPounds 
= 4000;
Vehicle.prototype.refuel 
= function()
{
    
return "Refueling Vehicle with regular 87 octane gasoline";
}
Vehicle.prototype.mainTasks 
= function()
{
    
return "Driving to  work ,school ,and the grocery store";
}
function SportsCar()
{
}
SportsCar.prototype 
= new Vehicle();
SportsCar.prototype.curbWeightInPounds 
= 3000;
SportsCar.prototype.refuel 
= function ()
{
    
return "Refueling SportsCar with premium 94 octane gasoline";
}
SportsCar.prototype.mainTasks 
= function(){
    
return "Spirited driving ,looking good ,driving to the beach";
}
function discribe(vehicle)
{
    
var str = "";
    str 
= "\n\nNumber of wheels:"+ vehicle.wheelCount;
    str 
+= "    Crub Weight: "+vehicle.curbWeightInPounds;
    str 
+= "\n\n Refuel Method:"+vehicle.refuel();
    str 
+= "\n\n Main Tasks:" + vehicle.mainTasks();
    document.getElementById(
"Info").innerText+=str;
}
function createVehicle()
{
    
var vehicle = new Vehicle();
    discribe(vehicle);
}
function createSportsCar()
{
    
var sportCar = new SportsCar();
    discribe(sportCar);
}

对象反射
检测一个对象是否有某个属性

if(typeof(MyObject.somePrototype)!="undefined")

检测对象某个属性的类型

if(myObj instanceof Array)

设置剪切板内容

copyLink:function()
    {
        
var link = window.location.href;
        
var endIndex = link.lastIndexOf("/")+1;
        
var host = link.substring(0,endIndex)+"bbs_big.aspx?topicId="+this.NewShowingTopicId;
        window.clipboardData.setData(
"Text",host);
        alert(
"已将链接["+host+"]复制到剪切板,粘贴即可!");
    }

get object position

 

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
function getPosition(e){
    
var left = 0;
    
var top  = 0;
    
while (e.offsetParent){
        left 
+= e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
        top  
+= e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
        e     
= e.offsetParent;
    }
    left 
+= e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
    top  
+= e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
    
return {x:left, y:top};
}

 将函数作为一个对象的方法调用

Function.apply()
function.apply(thisobj,args)

thisobj:调用函数的对象,在函数主体中,thisobj是关键字this的值;
args:一个数组,他的值是要传递给function的参数值
------------------
将函数作为对象的方法调用

Function.call()
function.call(thisobj,arg.)

thisobj:调用function的对象,在函数主体中,是this关键字的值;
args:任意多个参数,这些参数传递给函数function

【上篇】
【下篇】

抱歉!评论已关闭.