I have created an object literal using the code below. Everything works fine.
However, when I attempt to rewrite the object literal by creating an object constructor and a corresponding object, and then execute the method using the "dot syntax" nothing happens. I am unclear what I am doing wrong. The example below uses JQuery.
Thank you.
Object literal (working)
<!DOCTYPE=HTML>
<meta chartset="UTF-8">
<title> whatever </title>
<script type="text/javascript"> </script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" charset="utf-8"></script>
<div id="theDiv"></div>
<style>
#theDiv{
position:absolute;
width:200px;
height:200px;
background:#f00;
}
</style>
<script>
$(document).ready(function(){
var myObj = {};
myObj.doThing = function () {
$("#theDiv").toggle(3000);
};
myObj.doThing();
});
</script>
Constructor with object (non-working)
<!DOCTYPE=HTML>
<meta chartset="UTF-8">
<title> whatever </title>
<script type="text/javascript"> </script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" charset="utf-8"></script>
<div id="theDiv"></div>
<style>
#theDiv{
position:absolute;
width:200px;
height:200px;
background:#f00;
}
</style>
<script>
$(document).ready(function(){
function ConstructorExample (){
this.move = function () {
$("#theDiv".toggle(3000);
};
};
var objExample = new ConstructorExample();
objExample.move();
});
</script>