var a = new Object(); a.x = 1, a.y = 2;
var b = { x : 1, y : 2 };
function Point(x, y){
this.x = x;
this.y = y;
}
var p = new Point(1,2);
//创建一个空对象
var hero = {};
//为hero对象增加属性和方法
hero.name = "JavaScript";
hero.value = "helloworld";
hero.sayName = function(){return "hello " + hero.name;};
//测试
alert(hero.name); //output javascript
alert(hero.sayName()); //output hello javascript
//删除hero对象的name属性
delete hero.name;
//测试
alert(hero.sayName()); //output hello undefined
//创建一个空对象
var hero = {};
//为hero对象增加属性和方法
hero.name = "javascript";
hero.value = "helloworld";
hero.sayName = function(){return "hello " + this.name;};
//测试
alert(hero.name); //output javascript
alert(hero.sayName()); //output hello javascript
var object = {};
var obj = new Object();
var object = {};
var obj = new Array();
//反转字符串示例
//定义一个字符串
var str = "a,b,c,d,e,f,g";
//利用String对象的split()方法,将字符串切割成一个数组
var arr = str.split(",");
//利用Array对象的reverse()方法,将数组中元素的顺序颠倒。
arr = arr.reverse();
//测试打印
alert(arr.toString());
var str = "hello";
var obj = new String("world");
alert(typeof str); //typeof string
alert(typeof obj); //typeof object
//判断字符串是否包含指定字符串示例
//定义两个要判断的字符串
var str = "abcdefg";
var substr = "efg";
/*
* 定义判断字符串是否包含指定字符串的函数
* * 第一个参数:要判断的字符串
* * 第二个参数:指定的字符串
*/
function sub(str,substr){
//将判断的字符串定义成String对象
var string = new String(str);
//截取判断的字符串
var result = string.substr(string.indexOf(substr),substr.length);
/*
* 判断截取后的字符串是否为空
* * 为空,说明不包含指定字符串
* * 不为空,说明包含指定字符串
*/
if(result==substr){
return true;
}else{
return false;
}
}
alert(sub(str,substr));
function Hero(name, color){
this.name = name;
this.color = color;
this.whatareyou = function(){
return "I am a " + this.color + " " + this.name;
}
}
var hero = new Hero("javascript","red");
alert(hero.whatareyou()); //output I am a red javascript
Hero.prototype.price = 100;
Hero.prototype.rating = 3;
Hero.prototype.getInfo = function(){
return "Rating: " + this.rating + " , Price: " + this.price;
}
alert(hero.getInfo()); //output Rating: 3 , Price: 100
Hero.prototype = {
price : 100,
rating : 3,
getInfo : function(){
return "Rating: " + this.rating + " , Price: " + this.price;
}
};
function Hero(){
this.name = "jscript";
}
Hero.prototype.name = "javascript";
var hero = new Hero();
alert(hero.name); //output jscript
delete hero.name;
alert(hero.name); //output javascript
//为原型 Array对象增加一个判断的函数
Array.prototype.inArray = function(color){
for(var i = 0, len = this.length; i < len; i++){
if(this[i] === color){
return true;
}
}
return false;
}
//定义一个Array对象
var a = ["red", "green", "blue"];
//测试
alert(a.inArray("red")); //true
alert(a.inArray("yellow")); //false
function A(){
this.name = "a";
this.toString = function(){return this.name};
}
function B(){
this.name = "b";
}
function C(){
this.name = "c";
this.age = 18;
this.getAge = function(){return this.age};
}
B.prototype = new A();
C.prototype = new B();
function A(){}
A.prototype.name = "a";
A.prototype.toString = function(){return this.name};
function B(){}
B.prototype = A.prototype;
B.prototype.name = "b";
function C(){}
C.prototype = B.prototype;
C.prototype.name = "c";
C.prototype.age = 18;
C.prototype.getAge = function(){return this.age};
//该函数接受一个对象并返回它的副本
function extendCopy(p){
var z = {}; //定义一个空的对象z
for(var i in p){ //var i =0 ; i < p.length ; i++
z[i] = p[i]; //都当做数组处理的话,可以理解
}
//uber属性:将p作为z的父级,将z指向p的原型
z.uber = p;
return z;
}
//定义对象a,但是对象a不是函数对象
var a = {
name : "a",
toStr : function(){return this.name;}
}
//定义对象b,但是对象b不是函数对象
var b = extendCopy(a);
b.name = "b";
b.toStr = function(){return this.uber.toStr() + " , " + this.name;};
//定义对象c,但是对象c不是函数对象
var c = extendCopy(b);
c.name = 18;
alert(c.toStr()); //output a , b , 18
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有