JS Object-oriented practice

Source: Internet
Author: User


<script type= "Text/javascript" ></script>
<script type= "Text/javascript" >
function Student (name,age) {
This.name=name;
This.age=age;
}
student.hello= "Hello World";
Student.prototype.sayhello=function () {
Return this.name+ "--" +this.age+ "--" +student.hello;
};
var stu=new Student ("Zhang San", "14");
Alert (Stu.sayhello ());
</script>

<script type= "Text/javascript" >
var Student =function () {
var obj=new Object ();
obj.hello= "Hello World";
Obj.sayhello=function (name,age) {
Return name+ "--" +age+ "--" +this.hello;
}
return obj;
}
var stu=new Student ();
Alert (Stu.sayhello ("John Doe", "15"));
</script>

<script type= "Text/javascript" >
var student=new Object ();
student.hello= "Hello World";
Student.sayhello=function (name,age) {
Return name+ "--" +age+ "--" +this.hello;
}
Alert (Student.sayhello ("Harry", "16"));
</script>

<script type= "Text/javascript" >
var student={
Hello: "Hello World",
Sayhello:function (name,age) {
Return name+ "--" +age+ "--" +this.hello;
}
};
Alert (Student.sayhello ("Zhao Liu", "17"));
</script>

<script type= "Text/javascript" >
var student=new Function ("this.hello=\" helloworld\ "; this.sayhello=function (name,age) {return name+\"--\ "+age+\"--\ "+this.hello;}");
var stu=new Student ();
Alert (Stu.sayhello ("Chen Qi", "18"));
</script>

JS Object-oriented practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.