渡一 10 对象,包装类
2021/10/16 6:14:19
本文主要是介绍渡一 10 对象,包装类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
2.属性的增、删、改、查;
3.对象的创建方法;
1.字面量;
2.构造函数
系统自带: new Object();Array();Number();Boolean();String();Date()
自定义:function Person(){...}
3.object.create(原型)
eg.
var mrDeng = { name : "MrDeng", age : 40, sex : "male", health : 100, smoke : function(){ console.log("I am smoking! cool!"); this.health--; }, drink : function(){ console.log("I am drink"); this.health++; } }
传参
function Student(name,age,sex){ this.name = name; this.age = age; this.sex = sex; this.grade = 2017; } var student = new Student("zhangsan", 18, "male");
构造函数内部原理
1.在函数体最前面隐式加上this={};
2.执行 this.xxx = xxx;
3.隐匿的返回this
function Student(name,age,sex){ /* var this = { name : "", age : "" } */ this.name = name; this.age = age; this.sex = sex; this.grade = 2017; //return this; } var student = new Student("zhangsan", 18, "male");
包装类
var num = 4; num.len = 3; //new Number(4).len = 3; delete console.log(num.len); //undefined var str = "abc"; str += 1; var test = typeof(str); if(test.length == 6){ test.sign = "typeof的返回结果可能为String"; //new String(test).sign = "xxx"; } // new String(test).sign document.write(test.sign); //undefined var str = "abcd"; str.length = 2; //new String("abcd").length = 2;delete //new String("abcd").length console.log(str.length);
练习
1.运行test()和new test()的结果分别是什么? var a = 5; function test(){ a = 0; console.log(a); //0 console.log(this.a); //5 var a; console.log(a); //0 } test(); //0 5 0 var a = 5; function test(){ a = 0; console.log(a); //0 console.log(this.a); //this-->test{} this.a-->undefined var a; console.log(a); //0 } new test(); //0 undefined 0 5. function Person(){ var a = 0; function sss(){ a++; console.log(a); } this.say = sss; } var person = new Person(); person.say(); //1 这里是一个闭包 person.say(); //2 var person1 = new Person(); person1.say() //1 4.下面这段JS代码运行后x,y,z的值分别是多少? var x=1,y=z=0; function add(n){ return n=n+1; } y=add(x); function add(n){ //这个add会覆盖上面的 return n=n+3; } z=add(x) //1 4 4 6. function foo(){bar.apply(null,arguments)} function bar(x){console.log(arguments)} foo(1,2,3,4,5) //[1,2,3,4,5] function b(x,y,a){ arguments[2] = 10; alert(a); //10 } b(1,2,3); //改写 a = 10; console.log(arguments[2]); //10; arguments和变量是映射关系,但不相等
这篇关于渡一 10 对象,包装类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-30我的第一个Go命令行工具
- 2024-09-30初学者指南:轻松掌握模块化编程
- 2024-09-30顶级5款免费的IntelliJ插件,助你Java开发之路更顺畅
- 2024-09-30提高应用程序可用性:冗余和持久性
- 2024-09-30Twitter 系统设计面试示例
- 2024-09-30JSON对象入门教程:轻松掌握基础用法
- 2024-09-30封装入门:Java面向对象编程的第一步
- 2024-09-30后台交互入门:轻松掌握基础知识与实践技巧
- 2024-09-30轻松入门:后台交互教程详解
- 2024-09-30后台交互项目实战:新手指南