家人问你还没有对象怎么办?那你new一个对象呀

📒博客首页:酸狗的博客🍋
🎉欢迎关注🔎点赞👍收藏⭐️留言📝
💖热爱前端学习,期待一起交流!✨
🙏作者水平很有限,如果发现错误,求告知,多谢!🌈
😎有问题可私信我交流🙄

🤔万物皆对象

在这里插入图片描述

在JavaScript的世界,万物皆对象。除了null和undefined,其他基本类型数字,字符串和布尔值都有对应有包装对象。对象的一个特征是你可以在它身上直接调用方法。对于数字基本类型,当试图在其身上调用toString方法会失败,但用括号括起来后再调用就不会失败了,内部实现是用相应的包装对象将基本类型转为对象。所以(1).toString()相当于new Number(1).toString()。因此,你的确可以把基本类型数字,字符串,布尔等当对象使用的,只是注意语法要得体。

同时我们注意到,JavaScript中数字是不分浮点和整形的,所有数字其实均是浮点类型,只是把小数点省略了而以,比如你看到的1可以写成1.,这也就是为什么当你试图1.toString()时会报错,所以正确的写法应该是这样:1…toString(),或者如上面所述加上括号,这里括号的作用是纠正JS解析器,不要把1后面的点当成小数点。内部实现如上面所述,是将1.用包装对象转成对象再调用方法。

使用new则是将之当做构造函数来调用,会创建一个该类的实例对象,这个对象的type是这个函数名,中间使用this.propertyname=value会对该实例对象的属性赋值,并且无论有没有return,都会返回这个对象。

而如果是直接调用就是简单执行里面的代码,不会创建实例对象,this指向的调用环境下的this对象,如果是在全局中直接调用,就是window,没有默认返回值。

想要创建类,一般用前者(构造器模式),当然也可以使用后者,例如工厂模式和寄生构造器模式。

构造器模式:

function Person(name, age, job) {
		this.name = name;
		this.age = age;
		this.job = job;
		this.introduce = function() {
			alert("My name is " + this.name + ", I am" + age + "year(s) old, I am a " + job + ".");
		}
	}

工厂模式:

function createPerson(name, age, job) {
		var o = new Object();
		o.name = name;
		o.age = age;
		o.job = job;
		return o;
}

寄生构造器模式:

function SpecialArray() {
		var values = new Array();
		values.push.apply(values, arguments);
		values.toPipedString = function() {
			return this.join("|");
		};
		return values;
	}

后两种方法的缺点在于因为返回的还是object类型,所以不能通过instanceof来检测实际类型。

🤣New一个对象

xdm,家里人催你找对象了吗?
不要慌,身为程序员怎么能怕没对象?
没对象怎么办?new一个对象不就解决了吗?😍在这里插入图片描述

const obj = new Object;

✨new 操作符具体干了什么呢?

new操作符会做以下操作:

  1. 创建一个空的简单Javascript对象 (即{});

  2. 链接该对象(即设置该对象的构造函数)到另一个对象;

  3. 将步骤1新创建的对象作为this的上下文;

  4. 如果该函数没有返回对象,则返回this;

参考:MDN的new解释

🌈call() 和 apply() 的含义和区别?

首先说明两个方法的含义:

call:

  • 调用一个对象的一个方法,用另一个对象替换当前对象。例如:B. call(A, args1, args2); 即 A 对象调用 B 对象的方法。

apply:

  • 调用一个对象的一个方法,用另一个对象替换当前对象。例如:B. apply(A, arguments); 即 A 对象应用 B 对象的方法。

call 与 apply 的相同点:

  • 方法的含义是一样的,即方法功能是一样的;
  • 第一个参数的作用是一样的;

call 与 apply 的不同点:

  • 两者传入的列表形式不一样,call 可以传入多个参数;
    apply 只能传入两个参数,所以其第二个参数往往是作为数组形式传入

xdm,想一想哪个性能更好一些呢🤔?

在这里插入图片描述

🎉call实现不传对象

function myNew(fn) {
    // 以构造函数fn的prototype为原型 创建一个新的简单对象
    let o = Object.create(fn.prototype)
    // 改变fn的this指向到o,并执行fn
    let k = fn.call(o);
 
    // 如果构造函数没有返回对象,则返回o
    if (typeof k === 'object') {
        return k;
    } else {
        return o;
    }      
}

🎈apply 传递参数

function myNew(fn, ...args) {
    // 以构造函数fn的prototype为原型 创建一个新的简单对象
    let o = Object.create(fn.prototype)
    // 改变fn的this指向到o,并执行fn
    let k = fn.apply(o, args);
 
    // 如果构造函数没有返回对象,则返回o
    if (typeof k === 'object') {
        return k;
    } else {
        return o;
    }       
}

你现在可以说你有对象了😀
在这里插入图片描述

  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 26
    评论
以下是一个简单的电话薄程序的Java代码实现: ```java import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PhoneBook { private List<Contact> contacts; public PhoneBook() { contacts = new ArrayList<>(); } public void addContact(Contact contact) { contacts.add(contact); } public void removeContact(int index) { if (index >= 0 && index < contacts.size()) { contacts.remove(index); } } public void modifyContact(int index, Contact contact) { if (index >= 0 && index < contacts.size()) { contacts.set(index, contact); } } public Contact getContact(int index) { if (index >= 0 && index < contacts.size()) { return contacts.get(index); } return null; } public List<Contact> getAllContacts() { return contacts; } public void display() { for (Contact contact : contacts) { System.out.println(contact.toString()); } } public static void main(String[] args) { PhoneBook phoneBook = new PhoneBook(); Scanner scanner = new Scanner(System.in); int choice = 0; do { System.out.println("请选择操作:"); System.out.println("1. 添加联系人"); System.out.println("2. 删除联系人"); System.out.println("3. 修改联系人"); System.out.println("4. 查看指定联系人"); System.out.println("5. 查看所有联系人信息"); System.out.println("6. 退出程序"); System.out.print("请输入操作编号:"); choice = scanner.nextInt(); scanner.nextLine(); switch (choice) { case 1: System.out.print("请输入联系人类型(1:家人,2:朋友,3:工作联系人):"); int type = scanner.nextInt(); scanner.nextLine(); Contact contact = null; switch (type) { case 1: System.out.print("请输入姓名:"); String name = scanner.nextLine(); System.out.print("请输入电话:"); String phone = scanner.nextLine(); System.out.print("请输入备注:"); String remark = scanner.nextLine(); System.out.print("请输入地址:"); String address = scanner.nextLine(); System.out.print("请输入固定电话:"); String fixedPhone = scanner.nextLine(); contact = new Family(name, phone, remark, address, fixedPhone); break; case 2: System.out.print("请输入姓名:"); name = scanner.nextLine(); System.out.print("请输入电话:"); phone = scanner.nextLine(); System.out.print("请输入备注:"); remark = scanner.nextLine(); System.out.print("请输入类别(同学、同事、其他朋友):"); String category = scanner.nextLine(); contact = new Friend(name, phone, remark, category); break; case 3: System.out.print("请输入姓名:"); name = scanner.nextLine(); System.out.print("请输入电话:"); phone = scanner.nextLine(); System.out.print("请输入备注:"); remark = scanner.nextLine(); System.out.print("请输入公司:"); String company = scanner.nextLine(); System.out.print("请输入部门:"); String department = scanner.nextLine(); System.out.print("请输入职务:"); String position = scanner.nextLine(); contact = new WorkContact(name, phone, remark, company, department, position); break; default: System.out.println("输入有误,请重新输入!"); break; } if (contact != null) { phoneBook.addContact(contact); System.out.println("添加成功!"); } break; case 2: System.out.print("请输入要删除的联系人编号:"); int index = scanner.nextInt(); scanner.nextLine(); phoneBook.removeContact(index); System.out.println("删除成功!"); break; case 3: System.out.print("请输入要修改的联系人编号:"); index = scanner.nextInt(); scanner.nextLine(); System.out.print("请输入联系人类型(1:家人,2:朋友,3:工作联系人):"); type = scanner.nextInt(); scanner.nextLine(); switch (type) { case 1: System.out.print("请输入姓名:"); String name = scanner.nextLine(); System.out.print("请输入电话:"); String phone = scanner.nextLine(); System.out.print("请输入备注:"); String remark = scanner.nextLine(); System.out.print("请输入地址:"); String address = scanner.nextLine(); System.out.print("请输入固定电话:"); String fixedPhone = scanner.nextLine(); Contact newContact = new Family(name, phone, remark, address, fixedPhone); phoneBook.modifyContact(index, newContact); break; case 2: System.out.print("请输入姓名:"); name = scanner.nextLine(); System.out.print("请输入电话:"); phone = scanner.nextLine(); System.out.print("请输入备注:"); remark = scanner.nextLine(); System.out.print("请输入类别(同学、同事、其他朋友):"); String category = scanner.nextLine(); newContact = new Friend(name, phone, remark, category); phoneBook.modifyContact(index, newContact); break; case 3: System.out.print("请输入姓名:"); name = scanner.nextLine(); System.out.print("请输入电话:"); phone = scanner.nextLine(); System.out.print("请输入备注:"); remark = scanner.nextLine(); System.out.print("请输入公司:"); String company = scanner.nextLine(); System.out.print("请输入部门:"); String department = scanner.nextLine(); System.out.print("请输入职务:"); String position = scanner.nextLine(); newContact = new WorkContact(name, phone, remark, company, department, position); phoneBook.modifyContact(index, newContact); break; default: System.out.println("输入有误,请重新输入!"); break; } System.out.println("修改成功!"); break; case 4: System.out.print("请输入要查看的联系人编号:"); index = scanner.nextInt(); scanner.nextLine(); Contact targetContact = phoneBook.getContact(index); if (targetContact != null) { System.out.println(targetContact.toString()); } else { System.out.println("联系人不存在!"); } break; case 5: phoneBook.display(); break; case 6: System.out.println("退出程序,再见!"); break; default: System.out.println("输入有误,请重新输入!"); break; } } while (choice != 6); scanner.close(); } } class Contact { protected String name; protected String phone; protected String remark; public Contact(String name, String phone, String remark) { this.name = name; this.phone = phone; this.remark = remark; } @Override public String toString() { return "姓名:" + name + ",电话:" + phone + ",备注:" + remark; } } class Family extends Contact { private String address; private String fixedPhone; public Family(String name, String phone, String remark, String address, String fixedPhone) { super(name, phone, remark); this.address = address; this.fixedPhone = fixedPhone; } @Override public String toString() { return super.toString() + ",地址:" + address + ",固定电话:" + fixedPhone; } } class Friend extends Contact { private String category; public Friend(String name, String phone, String remark, String category) { super(name, phone, remark); this.category = category; } @Override public String toString() { return super.toString() + ",类别:" + category; } } class WorkContact extends Contact { private String company; private String department; private String position; public WorkContact(String name, String phone, String remark, String company, String department, String position) { super(name, phone, remark); this.company = company; this.department = department; this.position = position; } @Override public String toString() { return super.toString() + ",公司:" + company + ",部门:" + department + ",职务:" + position; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠檬树上柠檬果柠檬树下你和我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值