demo01
<html>
<head>
</head>
<body>
</body>
<script>
// 创建一个对象
var girlfriend = {
name : "初音未来",
age : 18 ,
shajiao : function(){
console.log("撒娇");
},
peiliaotian : function(){
console.log("陪聊天");
},
peiguangjie : function(){
console.log("陪逛街");
},
peikandianying :function(){
console.log("陪看电影");
}
};
console.log(girlfriend);
console.log(girlfriend.name);
console.log(girlfriend.age);
girlfriend.peiguangjie(); // 方法的调用
girlfriend.name = "欧阳娜娜" ;
console.log(girlfriend.name);
console.log(girlfriend.age);
girlfriend.peikandianying();
</script>
</html>
demo02
<html>
<head>
</head>
<body>
</body>
<script>
/*
工厂模式,实现批量创建对象
*/
// 定义一个函数
function GirlFriend ( name , age , func ){
var obj = {
gname : name,
gage : age,
gLiaotian : func
};
return obj;
}
var girlfriend01 = GirlFriend("初音未来" , 18 , function (){
console.log("一起吃饭");
});
var girlfriend02 = GirlFriend("欧阳娜娜" , 20 , function(){
console.log("一起看电影");
});
console.log("01号女性朋友:" , girlfriend01);
console.log("02号女性朋友:" , girlfriend02);
</script>
</html>
demo03
<html>
<head>
</head>
<body>
</body>
<script>
// 类 函数当作类使用 用函数描述特征
function Cat ( name , feature ){
this.name = name;
this.feature = feature;
this.info = function(){
console.log("名字:" , this.name , "特点:" , this.feature);
};
}
var lanmao = new Cat("蓝猫" , "毛是蓝色" );
lanmao.info();
var tianyuanmao = new Cat("中华田园猫" , "生命力强" );
tianyuanmao.info();
</script>
</html>
demo04函数作为对象方法
<html>
<head>
</head>
<body>
</body>
<script>
// 对象
var point = {
x : 0,
y : 0,
moveTo : function(newX , newY ) { // 1、函数moveTo 作为对象point 的方法
console.log("point中的this:" + this); // [object, object]
console.log("旧坐标:" , this.x , this.y , "新坐标:" , newX ,newY ); // 取对象的属性需要this
this.x = this.x + newX;
this.y = this.y + newY;
console.log("移动之后的点:" , this.x , this.y );
}
}
point.moveTo(10 , 10);
</script>
</html>
demo05函数作为构造方法
<html>
<head>
</head>
<body>
</body>
<script>
// 类 函数当作类使用 用函数描述特征
function Cat ( name , feature ){
this.name = name;
this.feature = feature;
this.info = function(){
console.log("名字:" , this.name , "特点:" , this.feature);
};
}
var lanmao = new Cat("蓝猫" , "毛是蓝色" );
lanmao.info();
var tianyuanmao = new Cat("中华田园猫" , "生命力强" );
tianyuanmao.info();
</script>
</html>
demo06 this作为全局变量
<html>
<head>
</head>
<body>
</body>
<script>
/*
*/
// this作为全局变量
console.log(this); // 表示当前窗口
function Fun01(){
console.log("普通函数中的this :" , this ); // 表示窗口对象
}
Fun01();
function Fun02() {
this.age = 18;
console.log("构造函数中的this :" , this ) // 表示的当前创建的对象
}
var obj = new Fun02();
</script>
</html>
demo07原型和原型链
<html>
<head>
</head>
<body>
</body>
<script>
function Person( name ){
this.name = name;
}
var a_person = new Person("欧阳娜娜");
console.log(a_person.constructor); // Person
console.log(Person.prototype); // Person.prototype
console.log(a_person.__proto__); // Person.prototype
console.log(Person.prototype.constructor); // Person
console.log(Person.prototype.__proto__); // Object
</script>
</html>
demo08 String对象
<html>
<head></head>
<body>
<input type="text" id="len">
<button onclick="getStringLength()"> 求长度 </button>
</body>
<script>
/*
String length属性 求长度
charAt
indexof
substring
split
toLowerCase
toUpperCase
*/
function getStringLength(){
var get_str = document.getElementById("len");
console.log(get_str.value); // 输入框里的值
let get_str_len = get_str.value.length; // length 求字符串的长度
console.log(get_str_len);
}
function testStringMethod(){
let str = "a good man is a Real man";
console.log(str.charAt(5)); // d 1、获取下标位置的元素 下标从0开始
console.log(str.indexOf("man")); // 7 2、判断子串在原字符串中首次出现的位置,如果没找到,返回-1
console.log(str.substring(2,6)); // 3、substring 截取函数 [包含开始位置,不包含结束位置)
console.log(str.split(" ")); // 4、切分函数 得到一个数组
console.log(str.toLowerCase() ); // 5、将字符串转为小写
console.log(str.toUpperCase() ); // 6、将字符全部转为大写
}
testStringMethod();
</script>
</html>
demo09 math对象
<html>
<head>
</head>
<body>
</body>
<script>
/*
round 四舍五入
random 随机数
floor 地板 向下取整
ceil 天花板 向上取整
*/
(function(){
console.log(Math.round(3.5) ); // 4
console.log(Math.round(3.4) ); // 3
console.log(Math.round(-3.5) ); // -3
console.log(Math.round(-3.4) ); // -3
console.log(Math.round(-3.6) ); // -4
console.log(Math.random() ); // [0,1) 之间的随机数
// [83,99) 区间长度 16 偏移量 83
// [0,1) [0,16) -> [83,99)
// 乘以区间长度+区间起始位置
console.log(Math.random() * 16 + 83 );
console.log(Math.floor(23.1) ); // 23
console.log(Math.floor(23.9) ); // 23
console.log(Math.floor(-23.1) ); // -24
console.log(Math.floor(-23.9) ); // -24
console.log(Math.ceil(23.1) ); // 24
console.log(Math.ceil(23.9) ); // 24
console.log(Math.ceil(-23.1) ); // -23
console.log(Math.ceil(-23.9) ); // -23
})();
</script>
</html>
demo09math对象
<html>
<head>
</head>
<body>
</body>
<script>
/*
round 四舍五入
random 随机数
floor 地板 向下取整
ceil 天花板 向上取整
*/
(function(){
console.log(Math.round(3.5) ); // 4
console.log(Math.round(3.4) ); // 3
console.log(Math.round(-3.5) ); // -3
console.log(Math.round(-3.4) ); // -3
console.log(Math.round(-3.6) ); // -4
console.log(Math.random() ); // [0,1) 之间的随机数
// [83,99) 区间长度 16 偏移量 83
// [0,1) [0,16) -> [83,99)
// 乘以区间长度+区间起始位置
console.log(Math.random() * 16 + 83 );
console.log(Math.floor(23.1) ); // 23
console.log(Math.floor(23.9) ); // 23
console.log(Math.floor(-23.1) ); // -24
console.log(Math.floor(-23.9) ); // -24
console.log(Math.ceil(23.1) ); // 24
console.log(Math.ceil(23.9) ); // 24
console.log(Math.ceil(-23.1) ); // -23
console.log(Math.ceil(-23.9) ); // -23
})();
</script>
</html>
demo10date对象
<html>
<head></head>
<body>
</body>
<script>
/*
getDate()
getDay()
getMinutes
getSeconds
getMonth
getFullYear
getTime
toDateString
toLocalDateString
toLocalTimeString
toLocalString
toString
toTimeString
toJSON()
*/
(function(){
var date = new Date();
console.log(date.getDate() ); // 29号
console.log(date.getDay() ) ; // 星期几
console.log(date.getMinutes() ); // 分钟
console.log(date.getSeconds() ); // 秒
console.log(date.getMonth() ); // 月 月份从0开始
console.log(date.getFullYear() ); // 年份 2020
console.log(date.getTime() ); // 毫秒数 从1970年1月1日开始
})();
</script>
</html>
demo11时间格式转换
<html>
<head>
</head>
<body>
<input type="button" name="btnMath" value="时间格式转换" onclick="mateMethod()">
</body>
<script>
function mateMethod(){
let timer01 = new Date();
let timer02 = new Date(1609227377988); // 给定一个时间戳,可以还原为当初的时间
let timer03 = new Date("2020-12-29"); // Tue Dec 29 2020 08:00:00 GMT+0800 (伊尔库茨克标准时间)
let timer04 = new Date(2002,11,30,1,12); // Mon Dec 30 2002 01:12:00 GMT+0800 (伊尔库茨克标准时间)
// console.log(timer01);
// console.log(timer02);
// console.log(timer03);
// console.log(timer04);
let timer05 = Date.parse(new Date("2020-12-29")); // 1609200000000
// console.log(timer05);
let timer06 = Date.UTC(2020, 12 ,29 ); // 1611878400000
// console.log(timer06);
let timer07 = Date.now(); // 1609228891754
// console.log(timer07);
console.log(timer01.toString()); //Tue Dec 29 2020 16:04:45 GMT+0800 (伊尔库茨克标准时间)
console.log(timer01.toTimeString()); // 16:04:45 GMT+0800 (伊尔库茨克标准时间)
console.log(timer01.toLocaleDateString()); // 2020/12/29
console.log(timer01.toLocaleTimeString()); // 下午4:04:45
console.log(timer01.toJSON()); // 2020-12-29T08:04:45.264Z
let date01 = new Date(2002 , 3 , 8);
let date02 = new Date(2020 , 12 , 29 );
let cha = date02-date01;
console.log(cha)
}
</script>
</html>