2020年12月13日 JS对象相关
<script type="text/javascript" src="attack.js"></script>
引入外部js,而不是用link
============================
string的replace方法也是用正则表达式
============================
关于Javascript对象:
内建对象(Math String Number Boolean Function Object)
宿主对象(BOM DOM 中的对象)
自定义对象(程序员自己写的)(实际工作中大部分时间不怎么用)
=-=================
删除对象属性:
delete obj1.name;
==================
var obj = new Object();
obj.name = "dog";
obj["name1"] = "cat";
document.write(obj["name1"]);
也可以操作属性 ,可以是字符串
========================
in运算符 可以来检查一个对象中是否有某个属性
var car1 = new Object();
var driver1 = new Object();
driver1.name = "Tom";
driver1.age = 26;
car1.name = "ford";
car1.driver = driver1;
document.write(car1.driver.age);
document.write("<br>");
document.write("driver" in car1);//true
instanceof 可以检查一个对象是否是一个类的实例
function Car(brand,color,speed){
this.brand = brand;
this.color = color;
this.speed = speed;
}
function Dog(breed,color,age){
this.breed = breed;
this.color = color;
this.age = age;
}
var ford = new Car("ford","red",160);
var bmw = new Car("bmw","blue",120)
console.log(ford instanceof Car);//true
console.log(ford instanceof Dog);//false
console.log(ford instanceof Object);//true 所有的对象都是Object的后代
==============================
引用数据类型:
var obj1 = new Object();
var obj2 = new Object();
obj1.name = "tom";
obj2 = obj1;
obj1.name = "bob";
document.write(obj1.name);
document.write(obj2.name);
基本数据类型的值直接保存在栈内存中
对象是保存在堆内存中,new就是在堆内存中开辟一个新空间用来保存对象。
对象变量里保存的是内存地址
比较基本数据类型的值,比较的是值。比较引用数据类型时,比较的是内存地址。
=================================
js中,函数也是对象。
匿名函数:
var func1 = function{
return 1;
}
===========================
js中,函数不会检查实参的类型和数量是否和形参一致,如果实参不够,剩下的以undefined传入
==============================
函数只写return ,或者不写return。相当于 return undefined
===============================
注意:函数中传入函数作为参数时,只写函数名,是传入函数对象;写函数名加小括号,是传入返回值
=================================
函数也可以作为返回值:
function print(input){
document.write(input);
document.write("<br>");
}
function fun1(i){
function fun11(){
print("11111111");
}
function fun12(){
print("2222222");
}
switch(i){
case 1:
return fun11;
case 2:
return fun12;
}
}
fun1(1)();
==================================
立即执行函数:(意义何在??)
function print(input){
document.write(input);
document.write("<br>");
}
(function(){
print("Apple");
})();
=======================
枚举对象中属性:
用 for in 语句
for(var i in document){
print(i);
}//每次执行,会将每个属性的属性名赋值给i
===============================
全局作用域:创建的变量都是window对象的属性
====================================
使用var关键字声明的变量,会在所有代码执行前被声明,但是不会赋值,到那一行再赋值
function f1(){
print(a);
var a = 567;
}
f1();//undefined
=======================================
调用函数时创建函数作用域,执行完毕以后销毁
==================================
在函数中不使用var声明的变量,会成为全局变量
=======================================
this是浏览器传入的函数 参数,指向的是函数执行的上下文对象。调用方式不同,this指向的也不同。
==============================================
构造函数:习惯上首字母大写 也成为类,构造函数创建的对象成为实例
function Car(brand,color,speed){
this.brand = ford;
this.color = color;
this.speed = speed;
}
var ford = new Car("ford","red",160);
var bmw = new Car("bmw","blue",120)
print(ford.color);
============================================
构造函数的优化:
function Square(inputlenth,inputwidth){
this.lenth = inputlenth;
this.width = inputwidth;
this.getSize = function(){
return this.lenth*this.width;
}
}
=====》》》》》》
function getSize1(){
return this.lenth*this.width;
}
function Square(inputlenth,inputwidth){
this.lenth = inputlenth;
this.width = inputwidth;
this.getSize = getSize1;
}
var square1 = new Square(3,4);
document.write(square1.getSize());
以上方法不安全,而且污染全局作用域
可以利用:
原型 prototype
每个函数都有一个叫prototype的属性,就是原型对象。
所有同一个类的实例都可以访问这个原型对象。
可以将对象共有的内容加进去。相当于公共区域。
如果原型里有了,但是自己又不想用,可以在自己里面再写一个覆盖掉。
function Square(inputlenth,inputwidth){
this.lenth = inputlenth;
this.width = inputwidth;
}
Square.prototype.getSize = function(){
return this.lenth*this.width;
}
var square1 = new Square(3,4);
document.write(square1.getSize());
这里注意区分
Square.prototype 类的原型
square1.__proto__实例的原型
原型也有的原型
Object的__proto__是null
=============================================
当直接打印对象时:
var obj1 = new Object();
console.log(obj1);
等同于
console.log(toString());
obj1.__proto__.__proto__.hasOwnProperty("toString");//true
(打印对象时,打印的是toString方法的返回值)
obj1.toString() = function(){
return "ABCABC";
}
(可以把实例的toString方法重写,这样打出来就不会是[object Object])
=================================================
内存回收:(垃圾回收 garbage cleaning)
JS引擎自动回收内存里的垃圾。
如果需要回收:
var obj = new Object();
obj = null;
设置为null即可。
===============================================
数组也是对象
从0开始的整数是它的索引

浙公网安备 33010602011771号