var json = {
a:12,
b:15,
c:'abc',
d:function(){
alert('a')
}
};
alert(json.a)//=>12
json.d();//=>a
var json = {
a:12,
show:function(){
alert(this);//=>object
console.log(this);
}
};
json.show();//当前方法的所属=》obj
var json = {
name:'nainai',
qq:'39456238',
showName:function(){
alert('My name :'+this.name);//=>My name :nainai
},
showQq:function(){
alert('My qq :'+this.qq);//=>My qq :39456238
}
};
json.showName();
json.showQq();
var zns = {};
zns.common = {};
zns.fx = {};
zns.site = {};
zns.common.getUser = function() {
alert('a');
};
zns.fx.getUser = function() {
alert('b');
};
zns.site.getUser = function() {
alert('c');
};
zns.common.getUser();
zns.fx.getUser();
zns.site.getUser();
对象:由属性和方法组成,继承:继承父级的属性、方法
function show(){
alert(this);
};
show();//=>window
show.call(12);//=>12
function show(a,b){
alert('this:'+this+'---a:'+a+'---b:'+b);
};
show(12,5);//=>this:window---a:12---b:15
show.call('this的值',12,5);//=>this:this的值---a:12---b:15
function A() {
this.abc = 12;
}
A.prototype.show = function() {
alert(this.abc);
}function B() {
//this->new B()
A.call(this);
};
var obj = new B();
alert(obj.abc);//=>12
function A() {
this.abc = 12;
}
A.prototype.show = function() {
alert(this.abc);
}
function B() {
//this->new B()
A.call(this);
}
B.prototype = A.prototype; //A的原型赋给B,A,B指向同一空间。
//给B的原型添加方法,就是给A也添加
// 当B有自己的方法
B.prototype.fn = function() {
alert('abc')
}
var objB = new B();
objB.show();
var objA = new A();
objA.fn();//=>abc
//此时A上有fn方法,此时看引用
var arr1=[1,2,3];
var arr2=arr1; //此时把1赋给2,arr2指向1所在的空间,没有创建新数组
arr2.push(4);
alert(arr1);//=>1,2,3,4
alert(arr2);//=>1,2,3,4
// 解决这个引用存在的问题
var arr1=[1,2,3];
var arr2=[]; //此时arr2指向1所在的空间,没有创建新数组
for(var i =0;i{
arr2.push (arr1[i]);
}
arr2.push(4);
alert(arr1);//1,2,3
alert(arr2);//1,2,3,4
function A() {
this.abc = 12;
}
A.prototype.show = function() {
alert(this.abc);
}
function B() {
//this->new B()
A.call(this);
}
for(var i in A.prototype){
B.prototype[i]=A.prototype[i];
}
// 当B有自己的方法
B.prototype.fn = function() {
alert('abc')
}
var objB = new B();
objB.show();
var objA = new A();
objA.fn();//=>objA.fn() is not a function
//此时,B有自己方法,而A没有B的fn
css
#div1 {
width: 200px;
height: 200px;
border: 1px solid #666;
position: absolute;
}
html
<div class="" id="div1">div>
js
window.onload = function() {
var oDiv = document.getElementById('div1');
oDiv.onmousedown = function(ev) {
var oEvent = ev || event;
var disX = oEvent.clientX - oDiv.offsetLeft;
var disY = oEvent.clientX - oDiv.offsetTop;
document.onmousemove = function(ev) {
var oEvent = ev || event;
oDiv.style.left = oEvent.clientX - disX + 'px';
oDiv.style.top = oEvent.clientY - disY + 'px';
};
document.onmouseup = function() {
document.onmousemove = null;
document.onmouseup = null;
}
};
};
var oDiv =null;
var disX = 0;
var disY =0;
window.onload = function() {
oDiv = document.getElementById('div1');
oDiv.onmousedown= fnDown;
};
function fnDown(ev) {
var oEvent = ev || event;
disX = oEvent.clientX - oDiv.offsetLeft;
disY = oEvent.clientX - oDiv.offsetTop;
document.onmousemove = fnMove;
document.onmouseup = fnUp;
};
function fnMove(ev) {
var oEvent = ev || event;
oDiv.style.left = oEvent.clientX - disX + 'px';
oDiv.style.top = oEvent.clientY - disY + 'px';
};
function fnUp() {
document.onmousemove = null;
document.onmouseup = null;
}
window.onload = function() { //初始化程序
new Drag('div1');
};
function Drag(id)
{
var _this=this;
this.disX=0;
this.disY=0;
this.oDiv=document.getElementById(id);
this.oDiv.onmousedown=function (ev)
{
_this.fnDown(ev);
return false;
};
};
Drag.prototype.fnDown=function (ev)
{
var _this=this;
var oEvent=ev||event;
this.disX=oEvent.clientX-this.oDiv.offsetLeft;
this.disY=oEvent.clientY-this.oDiv.offsetTop;
document.onmousemove=function (ev)
{
_this.fnMove(ev);
};
document.onmouseup=function ()
{
_this.fnUp();
};
};
Drag.prototype.fnMove=function (ev)
{
var oEvent=ev||event;
this.oDiv.style.left=oEvent.clientX-this.disX+'px';
this.oDiv.style.top=oEvent.clientY-this.disY+'px';
};
Drag.prototype.fnUp=function ()
{
document.onmousemove=null;
document.onmouseup=null;
};