[Front-end Development] Object-Oriented Programming Cases to Create Objects


<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title>
</head> <body> <button>one</button> <button>two</button> <button>three</button> <script src="https://topmdrt-static.oss-cn-shenzhen.aliyuncs.com/static/js/common/zepto.min.2.js"></script> <script type="text/javascript"> // object oriented programming to create an object function student(name, age) { this.name = name; this.hello = function() { if(age) { //There is an age parameter to execute console.log('hello' + name + 'I am' + age) } else { //There is no age parameter to execute console.log('hello' + name) } } } // student('xiaoming') //It is impossible to execute hello() in this way var child = new student('xiaohong'); //Reconstruct the function and call the parameter var child2 = new student('second function'); var child3 = new student('The third one', '26 years old'); child.hello(); //execute child2.hello(); child3.hello(); //Click event to pass parameters, click to trigger constructor and execution $('button').on('click', function() { var $this = $(this).text(); var child4 = new student('The fourth one', $this + 'year-old'); child4.hello(); }) //Create prototype-based JavaScript object student.prototype.say = function() { alert('Added successfully') } var say1 = new student('This is for testing'); say1.say(); </script> </body> </html>

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325132616&siteId=291194637