1.函数声明
function calsum1(num1, num2) { return num1 + num2; } console.log(calsum1(10, 10));
2.函数表达式
var calsum2 = function (num1, num2) { return num1 + num2; }
console.log(calsum2(10, 20));
3.函数对象方式
var calsum3 = new function('num1', 'num2', 'return num1 + num2'); console.log(calsum3(10, 30));
1.字面量方式
var student1 = { name: 'xiaofang', // 对象中的属性 age: 18, sex: 'male', sayhello: function () { console.log('hello,我是字面量对象中的方法'); }, dohomeword: function () { console.log("我正在做作业"); } }; console.log(student1); console.log(student1.name); student1.sayhello();
2.工厂模式创建对象
function createstudent(name, age, sex) { var student = new object(); student.name = name; student.age = age; student.sex = sex; student.sayhello = function () { console.log("hello, 我是工厂模式创建的对象中的方法"); } return student; } var student2 = createstudent('小红', 19, 'female'); console.log(student2); console.log(student2.name); student2.sayhello();
3.利用构造函数创建对象(常用)
function student (name, age, sex) { this.name = name; this.age = age; this.sex = sex; this.sayhello = function () { console.log("hello, 我是利用构造函数创建的对象中的方法"); } } var student3 = new student('小明', 20, 'male'); console.log(student3); console.log(student3.name); student3.sayhello();
对象代码运行结果
如对本文有疑问, 点击进行留言回复!!
javascript从入门到跑路-----小文的js学习笔记(19)------- js的垃圾回收机制
Agora 开源 | 一个 Demo,帮你快速实现社交直播四大场景
vuejs (2)- Vue 生命周期 和 VueJS ajax
网友评论