当前位置:网站首页>JS学习笔记09-12:原型对象以及Foreach+tostring及回收站
JS学习笔记09-12:原型对象以及Foreach+tostring及回收站
2022-07-17 07:28:00 【那人独钓寒江雪.】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> // //将函数定义在全局作用域,污染了全局作用域的命名空间 而且也不安全 // function Person(name,age,gender){
// this.name=name;
// this.age=age;
// this.gender=gender;
// }
// //向原型中添加sayName方法 // Person.prototype.sayName=function(){
// alert("Hello大家好,我是:"+this.name,"年龄为:"+this.age,"性别为:"+this.gender);
// };
// //创建一个Person的实例
// var per=new Person("孙悟空",18,"男")
// var per1=new Person("猪八戒",18,"男")
// per.sayName();
// per1.sayName();
//创建一个构造函数 function MyClass() {
}
//向MyClass的原型中添加一个name属性
MyClass.prototype.name = "我是原型中的名字";
var mc = new MyClass();
mc.age=18;
console.log(mc.name);
//可以使用对象的hasOwnProperty()来检查对象自身中是否含有该属性
//当我们使用一个对象的属性或方法时,会现在自身中寻找,Object对象的原型没用原型,如果在Object中依然没有找到,则返回undefined
console.log(mc.hasOwnProperty("age"));
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
//创建一个数组
var arr=["孙悟空","猪八戒","沙和尚","唐僧","白骨精"];
/* forEach()方法需要一个函数作为参数 我们创建但是不由我们调用,我们称为回调函数 以实参的形式传递进来,我们可以定义形参,浏览器会在回调函数中传递三个参数 第一个:正在遍历的元素,第二个,当前正在遍历元素的索引,第三个 正在遍历的数组 */
arr.forEach(function(value,index,obj){
console.log(value,index,obj);
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
//创建一个Person实例
var per = new Person("孙悟空", 19, "男");
// 当我们直接在页面中打印一个对象时,事件上是输出的对象的toString()方法的返回值
//如果我们希望在输出对象不输出[object object],可以为对象添加一个toString()方法
var result = per.toString();
//是否在原型对象中
console.log(per.__proto__.__proto__.hasOwnProperty("toString"))
//垃圾回收(GC)
// 我们需要一个垃圾回收机制,来处理程序运行过程中产生的垃圾
// 当一个对象没有任何变量或属性对它进行引用,此时我们将永远无法操作该对象,对象过多占用大量空间,导致内存运行延迟
//-在JS中有自动垃圾回收机制,不用也不需要进行回收操作
var obj=new Object();
//对对象进行各种操作... 将不使用的对象设置成null即可
obj=null;
</script>
</head>
<body>
</body>
</html>
边栏推荐
- Unity custom sky ball model to prevent it from being cropped
- 5.2 数据库安全
- [C # variable constant keyword] - variable constants and keywords in C #
- 5G正当时,无人驾驶未来将驶向何方?
- [C# Console]-C# 控制台类
- By voting for the destruction of STI by Dao, seektiger is truly community driven
- 从 B 站崩溃报告看分布式系统的技术栈
- Redis常用数据类型——Redis列表(List)和Redis 集合(Set)
- 真实案例:系统上线后Cpu使用率飙升如何排查?
- 畅玩JVM——关于GC垃圾回收必须要掌握的知识
猜你喜欢

leetcode:287. 寻找重复数【快慢指针板子】

3D laser slam:aloam --- interpretation of inter frame odometer code

Dependency injection method

Redis distributed lock

Enjoy JVM -- knowledge about GC garbage collection
![[C# 变量常量关键字]- C# 中的变量常量以及关键字](/img/9b/433f9110d9c7599d8beac8288ea409.png)
[C# 变量常量关键字]- C# 中的变量常量以及关键字

3D激光SLAM:ALOAM---帧间里程计代码解读

Ruffian Heng embedded bimonthly issue 58

总结的太好了!终于有人把SQL的各种连接Join都讲明白了

ObjectARX--自定义圆的实现
随机推荐
Local storage sessionstorage
Redis新数据类型——Bitmaps
[kernel] character device that drives development and learning
Standard Version (release and changelog Automation)
一款关于日常习惯打卡的小程序
Database review -- database recovery technology
通过ip获取归属地
The core problem of concurrent programming
Talk about distributed locks
WPF 三维应用搭建(基础)
5G正当时,无人驾驶未来将驶向何方?
Stm32f103c8t6 hardware IIC control 4-pin 0.96 inch OLED display
leetcode:287. 寻找重复数【快慢指针板子】
Filesourcestrategy, datasourcestrategy and datasourcev2strategy in spark
From the casino logic, analyze the investment value of platform currency 2020-03-03
RestTemplate
ansible自动化运维详解(四)ansible中playbook的编写使用、执行命令及实例演示
Go language Bible
5.1 vulnérabilités et précautions en matière de sécurité
5.2 数据库安全